A highly opinionated testing library for JavaScript
npm install javascript-testing-library --save-dev
{
"scripts": {
"test": "javascript-testing-library 'lib/**/*.test.js'"
}
}
// User.test.js
import { Suite } from 'javascript-testing-library'
import { User } from './User'
export const suite = Suite({
name: 'User',
})
suite.addTest({
name: 'can upsert a user with only a name',
async test(t) {
const user = User({
name: 'Justin'
})
const actual = await user.upsert()
t.equal({
expected: {
id: 1,
name: 'Justin'
},
actual
})
},
stubs: [
{
module: 'sequelize',
method: 'upsert',
returns(options) {
return {
id: 1,
name: options.name,
}
}
}
]
})
javascript-testing-library
is meant to force good testing practices onto a JavaScript application.
- Fast tests, because all expensive input and output are disabled.
- Focused tests, because only one assertion is allowed per-test.
- Useful tests, because it does not allow you to assert against the results of
typeof
. You have to assert against an actual value.
- Brittle tests, because it only allows you to stub external modules.
- Flakey tests, because it disables unreliable inputs and outputs like HTTP requests and file system operations.
- Skip a test.
- Have more than one assertion per-test.
- Use any assertion besides deep strict equality.
- Mock or stub internal modules.
- Make unmocked HTTP requests or file system operations.
- Use a setup/teardown construct that shares state between tests.
- Forget to restore a stubbed function, because they are automatically restored after each test.
Errors show a diff, including for objects an arrays. The stack trace will highlight the test file that the error happened in.
Currently averaging ~1300 tests per second. Hoping to have this at ~3k+ tests per second.
4004 Tests Passed in 3.369 seconds
- Pretty printed output
- Automatically stub node http
- Automatically stub node https
- Automatically stub node http/2
- Automatically stub node net
- Automatically stub node dns
- Automatically stub node tls
- Automatically stub node child_process
- Automatically stub node process
- Basic CLI
- Gracefully handle invalid test files
- Throw an error if a stub is not used by a test
- Don't allow a method to be stubbed twice in a test
- Built-in watch mode
- Some sort of multi-thread multi-process perf enhancement
- Find and run related tests