Skip to content

Commit

Permalink
test: configure jest
Browse files Browse the repository at this point in the history
  • Loading branch information
itsjavi committed Aug 8, 2023
1 parent 59762a8 commit 23134b3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
32 changes: 32 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @type {import('@jest/types').Config.InitialOptions}
*/
module.exports = {
coverageProvider: 'v8',
collectCoverageFrom: ['packages/*/src/**/*.{ts,tsx}'],
projects: [
{
displayName: 'DOM',
coveragePathIgnorePatterns: ['types.ts', 'index.ts', 'index.tsx', '/node_modules/'],
testEnvironment: './jest.env-browser.ts',
testMatch: ['**/*.test.ts', '**/*.test.tsx'],
testPathIgnorePatterns: ['/node_modules/', '/.local/', '/.ignore/', '/dist/'],
// setupFilesAfterEnv: ['./jest.setup-browser.js'],
transform: {
'^.+\\.(t|j)sx?$': ['@swc/jest'],
},
transformIgnorePatterns: ['/node_modules/', '^.+\\.module\\.(css|sass|scss)$'],
},
{
displayName: 'NodeJS',
coveragePathIgnorePatterns: ['types.ts', 'index.ts', '/node_modules/'],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testPathIgnorePatterns: ['/node_modules/', '/.local/', '/.ignore/', '/dist/', '.dom.test.ts'],
transform: {
'^.+\\.(t|j)s$': ['@swc/jest'],
},
transformIgnorePatterns: ['/node_modules/', '^.+\\.module\\.(css|sass|scss)$'],
},
],
}
28 changes: 28 additions & 0 deletions jest.env-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import JSDOMEnvironment from 'jest-environment-jsdom'

// https://github.com/facebook/jest/blob/v29.4.3/website/versioned_docs/version-29.4/Configuration.md#testenvironment-string
export default class BrowserEnvironment extends JSDOMEnvironment {
constructor(...args: ConstructorParameters<typeof JSDOMEnvironment>) {
super(...args)

// https://github.com/jsdom/jsdom/issues/1724#issuecomment-1446858041
this.global.fetch = fetch
this.global.Headers = Headers
this.global.Request = Request
this.global.Response = Response

// TextEncoder and TextDecoder are not available in JSDOM
const commonJsUtils = require('util')
this.global.TextEncoder = commonJsUtils.TextEncoder
this.global.TextDecoder = commonJsUtils.TextDecoder

// Some stream classes are different in JSDOM
const commonJsBuffer = require('buffer')
this.global.ArrayBuffer = ArrayBuffer
this.global.Blob = commonJsBuffer.Blob
this.global.File = commonJsBuffer.File

// URL implementation is different in JSDOM
this.global.URL = require('url').URL
}
}
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@
"prettier-fix": "prettier --write .",
"pretty": "pnpm run format",
"publint": "turbo publint-check",
"test": "turbo test",
"test.jest": "jest",
"test:ci": "turbo test:coverage",
"test:ci.jest": "jest --ci --coverage",
"test:coverage": "turbo test:coverage",
"test:coverage.jest": "jest --coverage",
"test": "jest --passWithNoTests",
"test:ci": "jest --ci --passWithNoTests --coverage",
"test:coverage": "jest --passWithNoTests --coverage",
"type-check": "turbo type-check"
},
"lint-staged": {
Expand Down

0 comments on commit 23134b3

Please sign in to comment.