Skip to content

Commit

Permalink
Chore: prevent package:test from using devdependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
pustovitDmytro committed May 10, 2021
1 parent 4acb941 commit dc896f3
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 283 deletions.
2 changes: 1 addition & 1 deletion .mocharc.bundle.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
"color": true,
"full-trace": true,
"recursive": true,
"require": ["./tests/init.js"]
"require": ["./tests-init.js"]
}
58 changes: 52 additions & 6 deletions bin/pack-tests.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,62 @@
#!./node_modules/.bin/babel-node

import path from 'path';
import { rollup } from 'rollup';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import json from '@rollup/plugin-json';
import multi from '@rollup/plugin-multi-entry';
import babel from '@rollup/plugin-babel';
import fs from 'fs-extra';
import packajeInfo from '../package.json';

const isMain = !module.parent;
const DIR = path.resolve('./tmp/package-tests');

const COPY = [
[ 'tests/init.js', 'tests-init.js' ],

[ '.mocharc.bundle.json', '.mocharc.json' ]
];

const TEST_MODULES = [
'mocha'
];

const resolveIgnoreRegexp = `^(?!${TEST_MODULES.join('|')}).*$`;

async function run(tarFilePath) {
const testConfig = {
'name' : `${packajeInfo.name}-tests`,
'version' : '1.0.0',
'scripts' : {
'test' : `ENTRY="./node_modules/${packajeInfo.name}/lib" mocha --config .mocharc.json tests.js`
},
'dependencies' : {
[packajeInfo.name] : path.resolve(tarFilePath)
},
'devDependencies' : TEST_MODULES.reduce((prev, cur) => ({
[cur] : packajeInfo.devDependencies[cur],
...prev
}), {})
};

async function run() {
try {
try {
await fs.remove(DIR);

const bundle = await rollup({
input : 'tests/**/*test.js',
plugins : [
babel({ exclude: 'node_modules/**' }),
resolve({ preferBuiltins: true }),
babel({
exclude : 'node_modules/**',
babelHelpers : 'inline'
}),
resolve({
preferBuiltins : true,
// eslint-disable-next-line security/detect-non-literal-regexp
resolveOnly : [ new RegExp(resolveIgnoreRegexp) ]
}),
commonjs({
include : [ /node_modules/ ],
sourceMap : false
Expand All @@ -29,11 +69,17 @@ async function run() {
]
});

console.log(bundle.watchFiles);
await fs.mkdir(DIR);
await bundle.write({
file : 'tmp/tests.js',
file : path.resolve(DIR, 'tests.js'),
format : 'cjs'
});

await Promise.all(COPY.map(([ from, to ]) => fs.copy(
from,
path.resolve(DIR, to)
)));
await fs.writeJSON(path.resolve(DIR, 'package.json'), testConfig);
} catch (error) {
console.error('ROLLUP ERROR');
throw error;
Expand All @@ -50,4 +96,4 @@ async function run() {
}


if (isMain) run();
if (isMain) run(process.argv[2]);
3 changes: 2 additions & 1 deletion bin/pack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ rm -rf tmp/package
PACKAGE="$(npm pack 2>&1 | tail -1)"
mkdir -p tmp
tar -xvzf $PACKAGE -C tmp
#mv $PACKAGE ${PACKAGE/-[0-9.]*.tgz/.tgz}
#mv $PACKAGE ${PACKAGE/-[0-9.]*.tgz/.tgz}
echo "$PACKAGE"
15 changes: 15 additions & 0 deletions bin/test-package.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e
echo "Cleaup ..."
rm -rf lib tmp/package

echo "Packing ..."
./bin/pack.sh
TAR_NAME="$(npm pack 2>&1 | tail -1)"
./bin/pack-tests.js $TAR_NAME

echo "Testing ..."

cd ./tmp/package-tests
npm i
npm test
Loading

0 comments on commit dc896f3

Please sign in to comment.