Skip to content

Instantly share code, notes, and snippets.

@hiptkang
Last active June 26, 2019 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiptkang/9f539a7fc8fb763950c6e40491833d2f to your computer and use it in GitHub Desktop.
Save hiptkang/9f539a7fc8fb763950c6e40491833d2f to your computer and use it in GitHub Desktop.
minimal-setup for jest and lint using babel
// .eslintrc.js
const config = {
"extends" : "airbnb",
"parser": "babel-eslint",
"rules": {
"no-plusplus": 2,
"no-console": "off",
},
"env": {
"jest": true
}
}
module.exports = config;
node_modules/
logs/
coverage/
npm-debug.log
.next
build/
.env
.DS_Store
.vscode
tmp/
tmp-*/
log/
test-results/
public/*bundle.*
// babel.config.js
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
$ yarn lint ./
yarn run v1.13.0
$ eslint --ignore-path .gitignore --ignore-pattern '**/dist/' --ext .jsx,.js ./
Done in 0.82s.
$ yarn test
yarn run v1.13.0
$ jest
Determining test suites to run...
# GLOBAL TEST SETUP #
PASS test ./hello.spec.js
✓ hello should return hello with my name (2ms)
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 0.399s, estimated 1s
Ran all test suites.
# GLOBAL TEST TEARDOWN #
Done in 0.99s.
// hello.js
const hello = name => `Hello, ${name}`;
module.exports = hello;
// hello.spec.js
import hello from './hello';
test('hello should return hello with my name', () => {
const res = hello('my name');
expect(res).toEqual('Hello, my name');
});
// jest.config.js
const ignoredPaths = [
'<rootDir>/node_modules/',
'<rootDir>/dist',
'<rootDir>/test/',
];
module.exports = {
displayName: 'test',
verbose: true,
testEnvironment: 'node',
testPathIgnorePatterns: ignoredPaths,
setupFilesAfterEnv: ['<rootDir>/test/setupTestFramework.js'],
globalSetup: '<rootDir>/test/setup.js',
globalTeardown: '<rootDir>/test/teardown.js',
moduleNameMapper: {
},
resetModules: false,
transform: {
'^.+\\.js?$': '<rootDir>/node_modules/babel-jest',
'^.+\\.ts?$': '<rootDir>/node_modules/babel-jest',
},
testRegex: '((\\.|/)(test|spec))\\.(js?|ts?)$',
moduleFileExtensions: ['ts', 'js'],
};
{
"name": "minsetup-jest-lint-babel",
"version": "1.0.0",
"description": "minimal setup for jest and lint using babel",
"main": "hello.js",
"scripts": {
"lint": "eslint --ignore-path .gitignore --ignore-pattern '**/dist/' --ext .jsx,.js",
"lint:fix": "npm run lint -- --fix",
"test": "jest"
},
"author": "hyunsamk",
"license": "ISC",
"dependencies": {
},
"devDependencies": {
"@babel/core": "^7.4.3",
"@babel/preset-env": "^7.4.5",
"babel-eslint": "^10.0.2",
"babel-jest": "^24.8.0",
"eslint": "^5.16.0",
"eslint-config-airbnb": "^17.1.0",
"eslint-plugin-import": "^2.17.0",
"jest": "^24.8.0",
"jest-junit": "^6.4.0"
}
}
// test/setup.js
module.exports = () => {
console.log('# GLOBAL TEST SETUP #');
};
// test/setupTe...js
// this file is ran right after the test framework is setup for some test file.
jest.setTimeout(5000);
// test/teardown.js
module.exports = () => {
console.log('# GLOBAL TEST TEARDOWN #');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment