Jest - sgml/signature GitHub Wiki
Syntax
- HTML attribute:
"data-testid"
- JavaScript object:
getbytestid
- node cli command:
node ./node_modules/jest/bin/jest.js <path-to-your-file>.js
Examples
Command Line
Use the following pattern: npx jest ./tests/myTest.spec.js src/mySource.js
Vue global test
test('Vue global object should have more than one key', () => {
let keyCount = 0;
for (let key in global.Vue) {
if (global.Vue.hasOwnProperty(key)) {
keyCount++;
}
}
expect(keyCount).toBeGreaterThan(1);
})
Vue compiler test
import { mount } from '@vue/test-utils';
import MyComponent from '@/components/MyComponent.vue';
describe('MyComponent', () => {
it('renders correctly', async () => {
const wrapper = mount(MyComponent);
await wrapper.vm.$nextTick();
expect(wrapper.find('div').text()).toBe('Expected Text');
});
});
Vue foobar test
Source file
// foo.js
function foo() {
return true;
}
module.exports = foo;
Test file
// bar.js
const foo = require('./foo');
test('bar', () => {
expect(foo()).toBe(true);
});
Runner
npx jest ./bar.js
Modulino Test
// foo-bar.js
function foo() {
return true;
}
function runTests() {
const result = foo();
if (result === true) {
console.log('Test passed');
} else {
console.log('Test failed');
}
}
// Only run tests if this script is executed directly
if (require.main === module) {
runTests();
}
module.exports = foo;
CoffeeScript
Transformer
// coffeeTransformer.js
const coffee = require('coffeescript');
module.exports = {
process(src, filename) {
return coffee.compile(src, { bare: true });
},
};
jest.config.js for transformer
// jest.config.js
module.exports = {
transform: {
'^.+\\.coffee$': 'coffeeTransformer.js',
'^.+\\.ssi$': 'ssiTransformer.js',
},
moduleFileExtensions: ['js', 'jsx', 'coffee', 'ssi'],
};
Test for window or globals
// env-check.js
function checkEnvironment() {
const env = {
hasGlobal: typeof global !== 'undefined',
hasWindow: typeof window !== 'undefined'
};
return env;
}
function runTests() {
let testFramework;
try {
// Attempt to require Jest
testFramework = require('@jest/globals');
} catch (e) {
// Fallback to Vitest if Jest is not available
testFramework = require('vitest');
}
const { describe, it, expect } = testFramework;
describe('Environment Check', () => {
it('should detect the global object', () => {
const env = checkEnvironment();
expect(env.hasGlobal).toBe(true);
});
it('should detect the window object', () => {
const env = checkEnvironment();
expect(env.hasWindow).toBe(true);
});
});
}
if (require.main === module) {
runTests();
}
module.exports = checkEnvironment;
References
- https://jest-bot.github.io/jest/docs/migration-guide.html
- https://dev.to/pustovalov_p/reducing-jest-memory-usage-1ina
- https://github.com/haoqunjiang/vite-jest#readme
- https://vitest.dev/guide/comparisons.html
- https://cathalmacdonnacha.com/migrating-from-jest-to-vitest
- jscodemods
- https://www.npmjs.com/package/jest-electron
- https://alfredo-perez.dev/setup-jest-in-angular-18
- https://blog.angular.dev/moving-angular-cli-to-jest-and-web-test-runner-ef85ef69ceca
- https://www.oreilly.com/library/view/modern-javascript-web/9781788992749/d1156d68-369f-4461-b3ad-b8fe4cecd9c7.xhtml
- https://jestjs.io/docs/configuration
- https://www.npmjs.com/package/jest-json
- https://ninkovic.dev/blog/2020/testing-web-components-with-jest-and-lit-element
- https://www.salesforceben.com/how-to-use-jest-for-lightning-web-component-testing/
- https://www.jeffryhouser.com/index.cfm/2022/10/5/How-do-I-test-a-private-static-method-in-TypeScript
- https://cheatcode.co/blog/how-to-write-test-and-publish-an-npm-package
- https://bambielli.com/til/2017-12-14-mocking-node-modules-with-jest/
- https://saucelabs.com/resources/blog/vitest-vs-jest-comparison