Skip to content

Commit

Permalink
feat(test): implement test script
Browse files Browse the repository at this point in the history
  • Loading branch information
zainfathoni committed Jun 14, 2022
1 parent dfafeac commit c729d12
Show file tree
Hide file tree
Showing 18 changed files with 533 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
...require('./src/config/eslintrc'),
overrides: [
{
files: ['babel.js', 'config.js', 'eslint.js', 'prettier.js'],
files: ['babel.js', 'config.js', 'eslint.js', 'jest.js', 'prettier.js'],
rules: {
'import/no-unresolved': 'off',
},
Expand Down
13 changes: 13 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { jest } = require('./src/config')

module.exports = {
...jest,
coverageThreshold: {
global: {
branches: 33,
functions: 50,
lines: 48,
statements: 48,
},
},
}
1 change: 1 addition & 0 deletions jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./src/config/jest.config')
17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"babel.js",
"config.js",
"eslint.js",
"jest.js",
"prettier.js"
],
"scripts": {
"test": "node src test",
"test:update": "node src test --updateSnapshot",
"build": "node src build",
"lint": "node src lint",
"format": "node src format",
Expand Down Expand Up @@ -54,6 +57,9 @@
"@types/arrify": "2.0.1",
"@types/babel__core": "7.1.19",
"@types/cross-spawn": "6.0.2",
"@types/is-ci": "3.0.0",
"@types/jest": "28.1.1",
"@types/jest-in-case": "1.0.5",
"@types/lodash.camelcase": "4.3.7",
"@types/lodash.has": "4.5.7",
"@types/lodash.omit": "4.5.7",
Expand All @@ -67,6 +73,7 @@
"@typescript-eslint/eslint-plugin": "5.27.1",
"@typescript-eslint/parser": "5.27.1",
"arrify": "2.0.1",
"babel-jest": "28.1.1",
"babel-plugin-macros": "3.1.0",
"babel-plugin-minify-dead-code-elimination": "0.5.2",
"babel-plugin-module-resolver": "4.1.0",
Expand All @@ -88,6 +95,12 @@
"eslint-plugin-react": "7.30.0",
"eslint-plugin-yml": "1.0.0",
"glob": "8.0.3",
"is-ci": "3.0.1",
"jest": "28.1.1",
"jest-environment-jsdom": "28.1.1",
"jest-serializer-path": "0.1.15",
"jest-snapshot-serializer-raw": "1.2.0",
"jest-watch-typeahead": "1.1.0",
"lodash.camelcase": "4.3.0",
"lodash.has": "4.5.2",
"lodash.omit": "4.5.0",
Expand All @@ -103,5 +116,9 @@
"semver": "7.3.7",
"which": "2.0.2",
"yargs-parser": "21.0.1"
},
"devDependencies": {
"jest-in-case": "1.0.2",
"slash": "3.0.0"
}
}
3 changes: 3 additions & 0 deletions src/__mocks__/cross-spawn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
sync: jest.fn(() => ({ status: 0 })),
}
48 changes: 48 additions & 0 deletions src/__tests__/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`format calls node with the script path and args including inspect-brk argument: format script 1`] = `node --inspect-brk=3080 <PROJECT_ROOT>/src/scripts/test.js --no-watch`;
exports[`format calls node with the script path and args: format script 1`] = `node <PROJECT_ROOT>/src/scripts/test.js --no-watch`;
exports[`format does not log for other signals: format signal 1`] = `Array []`;
exports[`format logs for SIGKILL signal: format signal 1`] = `
Array [
Array [
The script "lint" failed because the process exited too early. This probably means the system ran out of memory or someone called \`kill -9\` on the process.,
],
]
`;
exports[`format logs for SIGTERM signal: format signal 1`] = `
Array [
Array [
The script "build" failed because the process exited too early. Someone might have called \`kill\` or \`killall\`, or the system could be shutting down.,
],
]
`;
exports[`format logs help with no args: format snapshotLog 1`] = `
Array [
Array [
Usage: ../ [script] [--flags]
Available Scripts:
build
format
lint
test
typecheck
validate
Options:
All options depend on the script. Docs will be improved eventually, but for most scripts you can assume that the args you pass will be forwarded to the respective tool that's being run under the hood.
May the force be with you.
,
],
]
`;
exports[`format throws unknown script: format error 1`] = `Unknown script "unknown-script".`;
27 changes: 27 additions & 0 deletions src/__tests__/__snapshots__/utils.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`getConcurrentlyArgs gives good args to pass to concurrently 1`] = `
Array [
--kill-others-on-fail,
--prefix,
[{name}],
--names,
build,lint,test,validate,a,b,c,d,e,f,g,h,i,j,
--prefix-colors,
bgBlue.bold.white,bgGreen.bold.white,bgMagenta.bold.white,bgCyan.bold.white,bgWhite.bold.white,bgRed.bold.white,bgBlack.bold.white,bgYellow.bold.white,bgBlue.bold.white,bgGreen.bold.white,bgMagenta.bold.white,bgCyan.bold.white,bgWhite.bold.white,bgRed.bold.white,
"echo build",
"echo lint",
"echo test",
"echo validate",
"echo a",
"echo b",
"echo c",
"echo d",
"echo e",
"echo f",
"echo g",
"echo h",
"echo i",
"echo j",
]
`;
87 changes: 87 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import path from 'path'
import slash from 'slash'
import cases from 'jest-in-case' // TODO: replace jest-in-case with test.each()

const projectRoot = path.join(__dirname, '../../')

expect.addSnapshotSerializer({
print: (val) => slash(val.replace(projectRoot, '<PROJECT_ROOT>/')),
test: (val) => typeof val === 'string' && val.includes(projectRoot),
})

cases(
'format',
({ snapshotLog = false, throws = false, signal = false, args = [] }) => {
// beforeEach
const { sync: crossSpawnSyncMock } = require('cross-spawn')
const originalExit = process.exit
const originalArgv = process.argv
const originalLog = console.log
process.exit = jest.fn()
console.log = jest.fn()
try {
// tests
process.argv = ['node', '../', ...args]
crossSpawnSyncMock.mockClear()
if (signal) {
crossSpawnSyncMock.mockReturnValueOnce({ result: 1, signal })
}
require('..')
if (snapshotLog) {
expect(console.log.mock.calls).toMatchSnapshot('format snapshotLog')
} else if (signal) {
expect(process.exit).toHaveBeenCalledTimes(1)
expect(process.exit).toHaveBeenCalledWith(1)
expect(console.log.mock.calls).toMatchSnapshot('format signal')
} else {
expect(crossSpawnSyncMock).toHaveBeenCalledTimes(1)
const [firstCall] = crossSpawnSyncMock.mock.calls
const [script, calledArgs] = firstCall
expect([script, ...calledArgs].join(' ')).toMatchSnapshot(
'format script'
)
}
} catch (error) {
if (throws) {
expect(error.message).toMatchSnapshot('format error')
} else {
throw error
}
} finally {
// afterEach
process.exit = originalExit
process.argv = originalArgv
console.log = originalLog
jest.resetModules()
}
},
{
'calls node with the script path and args': {
args: ['test', '--no-watch'],
},
'calls node with the script path and args including inspect-brk argument': {
args: ['--inspect-brk=3080', 'test', '--no-watch'],
},
'throws unknown script': {
args: ['unknown-script'],
throws: true,
},
'logs help with no args': {
snapshotLog: true,
},
'logs for SIGKILL signal': {
args: ['lint'],
signal: 'SIGKILL',
},
'logs for SIGTERM signal': {
args: ['build'],
signal: 'SIGTERM',
},
'does not log for other signals': {
args: ['test'],
signal: 'SIGBREAK',
},
}
)

/* eslint complexity:0 */
Loading

0 comments on commit c729d12

Please sign in to comment.