Skip to content

Commit

Permalink
Fix jest tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitayutanov committed Sep 19, 2024
1 parent 8b5e0df commit 5a802d4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
1 change: 1 addition & 0 deletions js/cli/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function updateConfigVersions() {
config.versions['polkadot-api'] = sailsJs.peerDependencies['@polkadot/api'];
config.versions['sails-js'] = sailsJs.version;
config.versions['typescript'] = rootPkgJson.devDependencies.typescript;
config.versions['@gear-js/react-hooks'] = sailsJs.devDependencies['@gear-js/react-hooks'];

writeFileSync('src/config.json', JSON.stringify(config, null, 2));
},
Expand Down
23 changes: 7 additions & 16 deletions js/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import type { Config } from '@jest/types';
import { createDefaultEsmPreset } from 'ts-jest';

const config: Config.InitialOptions = {
...createDefaultEsmPreset(),
globalSetup: './test/setup.js',
clearMocks: true,
coverageProvider: 'v8',
testEnvironment: 'node',
verbose: true,

preset: 'ts-jest/presets/js-with-babel',
transformIgnorePatterns: ['node_modules/(?!@polkadot)/'],
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',

// manually resolving hooks to esm, cuz somehow jest resolves it to cjs?
// if main property in a package.json is pointing to esm, there's no problem whatsoever.
// maybe it has something to do with the package folder structure
'^@gear-js/react-hooks$': '<rootDir>/../node_modules/@gear-js/react-hooks/dist/esm/index.mjs',
},

testTimeout: 15_000,

// cuz hooks testEnvironment is jsdom. there was a problem with rxjs for example
// https://github.com/microsoft/accessibility-insights-web/pull/5421#issuecomment-1109168149
// https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28/#packagejson-exports
testEnvironmentOptions: {
customExportConditions: ['node'],
transform: {
'^.+\\.tsx?$': ['ts-jest', { useESM: true }],
},
testTimeout: 15_000,
testPathIgnorePatterns: ['demo-hooks.test.tsx'], // ignore hooks test
};

export default config;
4 changes: 2 additions & 2 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"scripts": {
"prebuild": "rm -rf lib",
"build": "npx rollup --config rollup.config.js",
"test": "yarn node --no-warnings --experimental-vm-modules $(yarn bin jest) --detectOpenHandles",
"test-hooks": "vitest"
"test-hooks": "vitest",
"test": "yarn node --no-warnings --experimental-vm-modules $(yarn bin jest) --detectOpenHandles && yarn run test-hooks"
},
"peerDependencies": {
"@gear-js/api": "0.38.2",
Expand Down
7 changes: 5 additions & 2 deletions js/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { defineConfig } from 'vitest/config';
export default defineConfig({
// resolving manually cuz vitest is using nodejs resolution, remove after hooks update
// https://github.com/vitest-dev/vitest/discussions/4233
resolve: { alias: { '@gear-js/react-hooks': '@gear-js/react-hooks/dist/esm/index.mjs' } },
resolve: {
alias: {
'@gear-js/react-hooks': '@gear-js/react-hooks/dist/esm/index.mjs',
},
},

test: {
// globalSetup: 'test/setup.js',
include: ['**/*-hooks.test.ts?(x)'], // targeting only hooks
environment: 'happy-dom', // faster than jsdom
watch: false, // fire one time
Expand Down

0 comments on commit 5a802d4

Please sign in to comment.