Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const path = require('path');
const glob = require('glob');
const fs = require('fs-extra');
const utils = require('@jupyterlab/buildutils/lib/utils');
// yarn add chai && yarn add --dev @types/chai
// Remove expect.js from package.json
const source = path.resolve('./test-apputils');
const pkgSrc = utils.readJSONFile(path.join(source, 'package.json'));
const chaiVer = pkgSrc.dependencies['chai'];
const chaiTypeVer = pkgSrc.devDependencies['@types/chai'];
const target = path.resolve(process.argv[2]);
if (!target) {
console.error('Specify a target dir');
process.exit(1);
}
const targetPkg = utils.readJSONFile(path.join(target, 'package.json'));
delete targetPkg.dependencies['expect.js'];
targetPkg.dependencies['chai'] = chaiVer;
targetPkg.devDependencies['@types/chai'] = chaiTypeVer;
utils.writePackageData(path.join(target, 'package.json'), targetPkg);
const utils = require('@jupyterlab/buildutils/lib/utils');
// yarn add chai && yarn add --dev @types/chai
// Remove expect.js from package.json
const source = path.resolve('./test-apputils');
const pkgSrc = utils.readJSONFile(path.join(source, 'package.json'));
const chaiVer = pkgSrc.dependencies['chai'];
const chaiTypeVer = pkgSrc.devDependencies['@types/chai'];
const target = path.resolve(process.argv[2]);
if (!target) {
console.error('Specify a target dir');
process.exit(1);
}
const targetPkg = utils.readJSONFile(path.join(target, 'package.json'));
delete targetPkg.dependencies['expect.js'];
targetPkg.dependencies['chai'] = chaiVer;
targetPkg.devDependencies['@types/chai'] = chaiTypeVer;
utils.writePackageData(path.join(target, 'package.json'), targetPkg);
glob.sync(path.join(target, 'src', '*.ts*')).forEach(function(filePath) {
let src = fs.readFileSync(filePath, 'utf8');
src = src
.split("import expect = require('expect.js')")
.join("import { expect } from 'chai'");
src = src.split('to.be(undefined)').join('to.be.undefined');
src = src.split('to.be(void 0)').join('to.be.undefined');
src = src.split('to.be(null)').join('to.be.null');
src = src.split('to.not.throwError(').join('to.not.throw(');
src = src.split('to.throwError(').join('to.throw(');
fs.writeFileSync(filePath, src, 'utf8');
});
// Create jest.config.js.
const jestConfig = `
const func = require('@jupyterlab/testutils/lib/jest-config');
module.exports = func('${name}', __dirname);
`;
fs.writeFileSync(path.join(testSrc, 'jest.config.js'), jestConfig, 'utf8');
// Open coreutils package.json
const coreUtils = path.resolve(__dirname, 'test-coreutils');
const coreUtilsData = require('./test-coreutils/package.json');
// Open target package.json
const targetData = utils.readJSONFile(path.join(testSrc, 'package.json'));
// Assign scripts from coreutils
targetData.scripts = coreUtilsData.scripts;
// Assign dependencies from coreutils
['jest', 'ts-jest', '@jupyterlab/testutils'].forEach(name => {
targetData.dependencies[name] = coreUtilsData.dependencies[name];
});
// Assign devDependencies from coreutils
targetData.devDependencies = coreUtilsData.devDependencies;
// Write out the package.json file.
utils.writeJSONFile(path.join(testSrc, 'package.json'), targetData);
// Update tsconfig to use jest types.
fs.writeFileSync(filePath, src, 'utf8');
});
// Create jest.config.js.
const jestConfig = `
const func = require('@jupyterlab/testutils/lib/jest-config');
module.exports = func('${name}', __dirname);
`;
fs.writeFileSync(path.join(testSrc, 'jest.config.js'), jestConfig, 'utf8');
// Open coreutils package.json
const coreUtils = path.resolve(__dirname, 'test-coreutils');
const coreUtilsData = require('./test-coreutils/package.json');
// Open target package.json
const targetData = utils.readJSONFile(path.join(testSrc, 'package.json'));
// Assign scripts from coreutils
targetData.scripts = coreUtilsData.scripts;
// Assign dependencies from coreutils
['jest', 'ts-jest', '@jupyterlab/testutils'].forEach(name => {
targetData.dependencies[name] = coreUtilsData.dependencies[name];
});
// Assign devDependencies from coreutils
targetData.devDependencies = coreUtilsData.devDependencies;
// Write out the package.json file.
utils.writeJSONFile(path.join(testSrc, 'package.json'), targetData);
// Update tsconfig to use jest types.
// Assign scripts from coreutils
targetData.scripts = coreUtilsData.scripts;
// Assign dependencies from coreutils
['jest', 'ts-jest', '@jupyterlab/testutils'].forEach(name => {
targetData.dependencies[name] = coreUtilsData.dependencies[name];
});
// Assign devDependencies from coreutils
targetData.devDependencies = coreUtilsData.devDependencies;
// Write out the package.json file.
utils.writeJSONFile(path.join(testSrc, 'package.json'), targetData);
// Update tsconfig to use jest types.
const tsData = utils.readJSONFile(path.join(testSrc, 'tsconfig.json'));
const index = tsData.compilerOptions.types.indexOf('mocha');
tsData.compilerOptions.types[index] = 'jest';
utils.writeJSONFile(path.join(testSrc, 'tsconfig.json'), tsData);
// Git remove old tests infra
['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(fname => {
utils.run(`git rm -f ./test-${name}/${fname} || true`);
});
// Copy common files from coreutils
['run.py', 'babel.config.js'].forEach(fname => {
fs.copySync(path.join(coreUtils, fname), path.join(testSrc, fname));
});
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Assign scripts from coreutils
targetData.scripts = coreUtilsData.scripts;
// Assign dependencies from coreutils
['jest', 'ts-jest', '@jupyterlab/testutils'].forEach(name => {
targetData.dependencies[name] = coreUtilsData.dependencies[name];
});
// Assign devDependencies from coreutils
targetData.devDependencies = coreUtilsData.devDependencies;
// Write out the package.json file.
utils.writeJSONFile(path.join(testSrc, 'package.json'), targetData);
// Update tsconfig to use jest types.
const tsData = utils.readJSONFile(path.join(testSrc, 'tsconfig.json'));
const index = tsData.compilerOptions.types.indexOf('mocha');
tsData.compilerOptions.types[index] = 'jest';
utils.writeJSONFile(path.join(testSrc, 'tsconfig.json'), tsData);
// Git remove old tests infra
['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(fname => {
utils.run(`git rm -f ./test-${name}/${fname}`);
});
// Copy run.py from coreutils
fs.copySync(path.join(coreUtils, 'run.py'), path.join(testSrc, 'run.py'));
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Update deps and build test
// Git remove old tests infra
['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(fname => {
utils.run(`git rm -f ./test-${name}/${fname} || true`);
});
// Copy common files from coreutils
['run.py', 'babel.config.js'].forEach(fname => {
fs.copySync(path.join(coreUtils, fname), path.join(testSrc, fname));
});
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Update deps and build test
utils.run('jlpm && jlpm build', { cwd: testSrc });
// Test
utils.run('jlpm test', { cwd: testSrc });
tsData.compilerOptions.types[index] = 'jest';
utils.writeJSONFile(path.join(testSrc, 'tsconfig.json'), tsData);
// Git remove old tests infra
['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(fname => {
utils.run(`git rm -f ./test-${name}/${fname}`);
});
// Copy run.py from coreutils
fs.copySync(path.join(coreUtils, 'run.py'), path.join(testSrc, 'run.py'));
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Update deps and build test
utils.run('jlpm && jlpm build', { cwd: testSrc });
// Test
utils.run('jlpm test', { cwd: testSrc });
// Git remove old tests infra
['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(fname => {
utils.run(`git rm -f ./test-${name}/${fname}`);
});
// Copy run.py from coreutils
fs.copySync(path.join(coreUtils, 'run.py'), path.join(testSrc, 'run.py'));
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Update deps and build test
utils.run('jlpm && jlpm build', { cwd: testSrc });
// Test
utils.run('jlpm test', { cwd: testSrc });
utils.run(`git rm -f ./test-${name}/${fname} || true`);
});
// Copy common files from coreutils
['run.py', 'babel.config.js'].forEach(fname => {
fs.copySync(path.join(coreUtils, fname), path.join(testSrc, fname));
});
// Add new files to git
utils.run(`git add ./test-${name}/run.py ./test-${name}/jest.config.js`);
// Update deps and build test
utils.run('jlpm && jlpm build', { cwd: testSrc });
// Test
utils.run('jlpm test', { cwd: testSrc });