Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update tests to run without need to build them before #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .jestrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/scripts/test/jest.config.library');
5,751 changes: 2,336 additions & 3,415 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"homepage": "https://github.com/exosjs/exos-scripts#readme",
"scripts": {
"lint": "eslint --ext .js --ext .ts src/",
"test": "npm run build && jest --config=./src/scripts/test/jest.config.library.js --coverage=false",
"test": "jest --config=.jestrc.js",
"build": "rm -rf lib && tsc",
"prepublishOnly": "npm run build",
"publish": "semantic-release"
Expand Down Expand Up @@ -60,7 +60,7 @@
"stylelint": "^13.6.0",
"stylelint-config-property-sort-order-smacss": "^6.3.0",
"stylelint-config-standard": "^20.0.0",
"ts-jest": "^26.0.0",
"ts-jest": "^26.1.4",
"ts-loader": "^6.2.2",
"tslib": "^2.0.0",
"url-loader": "^4.0.0",
Expand All @@ -78,6 +78,7 @@
"@types/webpack": "^4.41.10",
"@types/webpack-dev-server": "^3.10.1",
"semantic-release": "^17.0.8",
"ts-node": "^8.10.2",
"typescript": "^3.9.3",
"webpack-cli": "^3.3.11"
}
Expand Down
10 changes: 3 additions & 7 deletions src/exos-scripts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import chalk from 'chalk';
import path from 'path';
import spawn from 'cross-spawn';
import { ExosScripts } from './common/types';

export default function exosScripts(scriptName: string, args: string[]): void {
export default function exosScripts(scriptName: string): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you removing the args?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because currently there is 2 process spawns. It should be improved but in different PR. Now I'm doing this minimum to make it possible for tests to work without prebuild.

Copy link
Collaborator

@interactivellama interactivellama Aug 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually babel, transpile, etc. is handled by Jest setup. However once we have functional unit tests, we probably will want to build for that script such as npm run test:functional or npm run test:browser.

Would a npm run test:unit without a build step be a good option?

if (!(scriptName in ExosScripts)) {
console.log(chalk.red(`Script ${scriptName} doesn't exist.`));
console.log(chalk.red(`Valid scripts are: ${Object.keys(ExosScripts).join(', ')}.`));
Expand All @@ -14,8 +12,6 @@ export default function exosScripts(scriptName: string, args: string[]): void {
console.log(`Executing script ${scriptName}...`);
console.log();

const scriptPath = require.resolve(path.resolve(__dirname, 'scripts', `${scriptName}/${scriptName}`));
const result = spawn.sync('node', [scriptPath, ...args], { stdio: 'inherit' });

process.exit(result.status || undefined);
// eslint-disable-next-line
require(`./scripts/${scriptName}/${scriptName}`);
}
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env node
import exosScripts from './exos-scripts';

const scriptToExecute = process.argv[2];
const otherArgs = process.argv.slice(3);
const script = process.argv[2];

exosScripts(scriptToExecute, otherArgs);
exosScripts(script);
5 changes: 3 additions & 2 deletions src/scripts/test/jest.config.library.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '^.+\\.(tests?|spec)\\.[jt]sx?$',
testMatch: ['**/*.spec.[jt]s?(x)'],
testPathIgnorePatterns: ['/lib/', '/dist/', '/node_modules/'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
collectCoverage: !!process.env.CI,
collectCoverageFrom: ['**/*.{js,jsx,tsx,ts}', '!**/node_modules/**'],
collectCoverageFrom: ['./src/**/*.[jt]s?(x)'],
moduleNameMapper: {
'\\.(css|less|scss)$': 'identity-obj-proxy',
'\\.(jpg|jpeg|png|svg)$': path.join(__dirname, 'FileMock.js'),
Expand Down
10 changes: 5 additions & 5 deletions test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import spawn from "cross-spawn";
import path from "path";
import type { SpawnSyncReturns } from "child_process";
import spawn from 'cross-spawn';
import path from 'path';
import type { SpawnSyncReturns } from 'child_process';

const EXOS_SCRIPT_PATH = path.resolve(process.cwd(), "./lib/index.js");
const EXOS_SCRIPT_PATH = path.resolve(process.cwd(), './src/index.ts');

function runScript(scriptPath: string, args: string[]): SpawnSyncReturns<string> {
return spawn.sync("node", [scriptPath, ...args], { encoding: "utf8" });
return spawn.sync('ts-node', [scriptPath, ...args], { encoding: 'utf8' });
}

function runExosScript(args: string[]): SpawnSyncReturns<string> {
Expand Down