Skip to content

Commit 3496718

Browse files
authored
feat(command): Fail tests under certain conditions (#40)
A unit test will fail when: - A test case doesn't have any assertions (typically broken async tests) - A `console.error` or `console.warn` happens during test case run BREAKING CHANGE: This change is non-backwards since it'll likely create new test failures
1 parent cae7002 commit 3496718

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/commands/test/project-unit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = {
1111
// is just looking at *.ts
1212
testMatch: [resolve(ROOT_DIR, 'src/**/*.spec.ts')],
1313

14+
setupFilesAfterEnv: [resolve(__dirname, 'unit-setup.js')],
15+
1416
// TODO: Use setupTestFrameworkScriptFile to ensure an expect runs for each test
1517
// Possibly also prevent console.error / console.warn
1618
}

src/commands/test/unit-setup.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
beforeEach(() => {
2+
expect.hasAssertions()
3+
})
4+
5+
const CONSOLE_FAIL_TYPES = ['error', 'warn']
6+
7+
// Throw errors when a `console.error` or `console.warn` happens
8+
CONSOLE_FAIL_TYPES.forEach((type) => {
9+
// eslint-disable-next-line no-console
10+
console[type] = (message) => {
11+
throw new Error(`Failing due to console.${type} while running test!\n\n${message}`)
12+
}
13+
})

0 commit comments

Comments
 (0)