convert-to-jest.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const path = require('path');
  2. const glob = require('glob');
  3. const fs = require('fs-extra');
  4. const utils = require('@jupyterlab/buildutils');
  5. const target = process.argv[2];
  6. if (!target) {
  7. console.error('Specify a target dir');
  8. process.exit(1);
  9. }
  10. // Make sure tests folder exists
  11. let testSrc = path.join(__dirname, '..', 'tests', 'test-' + target);
  12. console.log(testSrc); // eslint-disable-line
  13. if (!fs.existsSync(testSrc)) {
  14. console.log('bailing'); // eslint-disable-line
  15. process.exit(1);
  16. }
  17. // Update the test files
  18. glob.sync(path.join(testSrc, 'src', '**', '*.ts*')).forEach(function(filePath) {
  19. console.log(filePath); // eslint-disable-line
  20. // Convert test files to use jest
  21. let src = fs.readFileSync(filePath, 'utf8');
  22. src = src.split('before(').join('beforeAll(');
  23. src = src.split('context(').join('describe(');
  24. src = src.split('after(').join('afterAll(');
  25. // Use imports from /src
  26. src = src
  27. .split(`'@jupyterlab/${target}';`)
  28. .join(`'@jupyterlab/${target}/src';`);
  29. fs.writeFileSync(filePath, src, 'utf8');
  30. });
  31. // Create jest.config.js.
  32. const jestConfig = `
  33. const func = require('@jupyterlab/testutils/lib/jest-config');
  34. module.exports = func(${target}, __dirname);
  35. ```;
  36. fs.writeFileSync(path.join(testSrc, 'jest.config.js'), jestConfig, 'utf8');
  37. // Open coreutils package.json
  38. const coreUtils = path.resolve(__dirname, '..', 'tests', 'test-coreutils');
  39. const coreUtilsData = require('../tests/test-coreutils/package.json');
  40. // Open target package.json
  41. const targetData = require(`../tests/test-${target}/package.json`);
  42. // Assign scripts from coreutils
  43. targetData.scripts = coreUtilsData.scripts;
  44. // Assign dependencies from coreutils
  45. ['jest', 'ts-jest', '@jupyterlab/testutils'].forEach(name => {
  46. targetData.dependencies[name] = coreUtilsData.dependencies[name];
  47. });
  48. // Assign devDependencies from coreutils
  49. targetData.devDependencies = coreUtilsData.devDependencies;
  50. // Write out the package.json file.
  51. utils.writeJSONFile(path.join(testSrc, 'package.json'), targetData);
  52. // Git remove old tests infra
  53. ['karma-cov.conf.js', 'karma.conf.js', 'run-test.py'].forEach(name => {
  54. utils.run(`git rm -f ../tests/test-${target}/${name}`);
  55. });
  56. // Copy run_test.py from coreutils
  57. fs.copySync(
  58. path.join(coreUtils, 'run_test.py'),
  59. path.join(testSrc, 'run_test.py')
  60. );
  61. // Update deps and build all
  62. utils.run('jlpm && jlpm build:packages', {
  63. cwd: path.resolve(__dirname, '..')
  64. });
  65. // Test
  66. utils.run('jlpm test', { cwd: testSrc });