Skip to content

Commit

Permalink
test: add e2e test case for ESM features
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnpnl committed Aug 19, 2024
1 parent 978169b commit 00550ef
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 0 deletions.
34 changes: 34 additions & 0 deletions e2e/esm-features/__tests__/esm-features.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import jsonImportAssertion from '../src/foo.json' assert { type: 'json' }

const topLevelAwait = await import('../src/foo.json')

const itWithRunNode18Above = (...args: Parameters<typeof it>) => {
const [major] = process.versions.node.split('.').map(Number)
if (major > 16) {
// eslint-disable-next-line jest/valid-title,jest/expect-expect,jest/no-disabled-tests
return it(...args)
}

// eslint-disable-next-line jest/valid-title,jest/expect-expect,jest/no-disabled-tests
return it.skip(...args)
}

describe('esm-features', () => {
it('should work with import.meta', () => {
expect(import.meta.jest).toBeDefined()
})

it('should work with import assertion', () => {
expect(jsonImportAssertion.name).toBe('hello')
})

itWithRunNode18Above('should work with import attributes', async () => {
const jsonImportAttrs = await import('../src/foo.json', { with: { type: 'json' } })
// eslint-disable-next-line jest/no-standalone-expect
expect(jsonImportAttrs.default.name).toBe('hello')
})

it('should work with top-level await', () => {
expect(topLevelAwait.default.name).toBe('hello')
})
})
15 changes: 15 additions & 0 deletions e2e/esm-features/jest-compiler-esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type JestConfigWithTsJest, TS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'esm-features-compiler-esm',
extensionsToTreatAsEsm: ['.ts'],
transform: {
[TS_TRANSFORM_PATTERN]: [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
useESM: true,
},
],
},
} satisfies JestConfigWithTsJest
16 changes: 16 additions & 0 deletions e2e/esm-features/jest-transpiler-esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { type JestConfigWithTsJest, TS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'esm-features-transpiler-esm',
extensionsToTreatAsEsm: ['.ts'],
transform: {
[TS_TRANSFORM_PATTERN]: [
'ts-jest',
{
tsconfig: '<rootDir>/tsconfig-esm.spec.json',
isolatedModules: true,
useESM: true,
},
],
},
} satisfies JestConfigWithTsJest
4 changes: 4 additions & 0 deletions e2e/esm-features/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "esm-features",
"private": true
}
3 changes: 3 additions & 0 deletions e2e/esm-features/src/foo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "hello"
}
3 changes: 3 additions & 0 deletions e2e/esm-features/tsconfig-esm.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.json"
}
3 changes: 3 additions & 0 deletions e2e/esm-features/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig-esm.spec.json"
}

0 comments on commit 00550ef

Please sign in to comment.