- Install node https://nodejs.org/en/download/
$ node -v
v10.16.0
- Install and npm https://www.npmjs.com/get-npm
$ npm -v
6.9.0
$ mkdir tdd-roman-numerals-kata
$ cd tdd-roman-numerals-kata
Press ↩️ at each prompt to accept the default response
$ npm init
...
package name: (tdd-roman-numerals-kata)
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
...
Is this OK? (yes)
Add "private": true
to package.json to npm will refuse to publish it (this is a way to prevent accidental publication of private repositorie)
Use npm to install the jest package
$ npm install --save-dev [email protected]
npm notice created a lockfile as package-lock.json. You should commit this file.
+ [email protected]
added 479 packages from 344 contributors and audited 873786 packages in 12.856s
found 0 vulnerabilities
npm is a package manager, npx is a package runner.
$ npx jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In ~/tdd-roman-numerals-kata
2 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
testPathIgnorePatterns: /node_modules/ - 2 matches
testRegex: - 0 matches
Pattern: - 0 matches
$ npx jest --version
24.8.0
The best way to do that is to list those commands in the scripts section of your project’s package.json
.
{
"name": "tdd-roman-numerals-kata",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "jest"
},
"author": "",
"license": "ISC",
"devDependencies": {
"jest": "^24.8.0"
}
}
$ npm test
> [email protected] test ~/tdd-roman-numerals-kata
> jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In ~/tdd-roman-numerals-kata
2 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
testPathIgnorePatterns: /node_modules/ - 2 matches
testRegex: - 0 matches
Pattern: - 0 matches
npm ERR! Test failed. See above for more details.
Create a file called src/tests/romanNumerals.test.js :
const arabicToRoman = arabic => `I`;
describe('arabicToRoman()', () => {
it('convert number one', () => {
expect(arabicToRoman(1)).toBe('I');
});
});
Run all tests with:
$ npm test
...
PASS src/tests/romanNumerals.test.js
arabicToRoman()
✓ convert number one (2ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.624s, estimated 1s
Ran all test suites.
Create src/romanNumerals.js file:
module.exports = (number) => 'I';
And chage the src/tests/romanNumerals.test.js:
const arabicToRoman = require('../romanNumerals');
describe('arabicToRoman()', () => {
it('convert number one', () => {
expect(arabicToRoman(1)).toBe('I');
});
it('convert number two', () => {
expect(arabicToRoman(2)).toBe('II');
});
});
$ npx jest --watchAll
$ npm run test -- --coverage --coverageReporters=text