Skip to content

Instantly share code, notes, and snippets.

@gmahomarf
Last active October 3, 2022 12:46
Show Gist options
  • Save gmahomarf/c60e05ef57c35aad8e18590df8e3f945 to your computer and use it in GitHub Desktop.
Save gmahomarf/c60e05ef57c35aad8e18590df8e3f945 to your computer and use it in GitHub Desktop.
esmock strict mode test
export default () => {
return 'default'
}
export function extra() {
return 'extra'
}
import process from 'node:process'
import def, { extra } from './import1.js'
export function getDefault() {
return def()
}
export function getExtra() {
return extra()
}
export function getEnv(key) {
return process.env[key]
}
export function getCwd() {
return process.cwd()
}
import assert from 'node:assert'
import { strict as esmock } from 'esmock'
describe('esmock strict mode test', () => {
describe('full definition', () => {
let main
beforeEach(async () => {
main = await esmock('./main.js', {
'./import1': {
default: () => 'mocked def',
extra: () => 'mocked extra',
},
'node:process': {
default: {
cwd: () => 'here',
env: {
key1: 'env1',
key2: 'env2',
}
}
}
})
})
it('should mock all imports', async () => {
assert.equal(main.getDefault(), 'mocked def')
assert.equal(main.getExtra(), 'mocked extra')
assert.equal(main.getEnv('key1'), 'env1')
assert.equal(main.getEnv('key2'), 'env2')
assert.equal(main.getCwd(), 'here')
})
})
describe('partial definition of one import', () => {
describe('default export undefined', () => {
it('should fail esmock call due to missing default export', async () => {
await assert.rejects(esmock('./main.js', {
'./import1': {
extra: () => 'mocked extra',
},
'node:process': {
default: {
cwd: () => 'here',
env: {
key1: 'env1',
key2: 'env2',
}
}
}
}), 'import should fail due to missing default export')
})
})
describe('named export missing', () => {
it('should fail esmock call due to missing named export', async () => {
await assert.rejects(esmock('./main.js', {
'./import1': {
default: () => 'mocked default',
},
'node:process': {
default: {
cwd: () => 'here',
env: {
key1: 'env1',
key2: 'env2',
}
}
}
}), 'import should fail due to missing named export')
})
})
})
describe('missing imported module definition', () => {
it('should fail esmock call due to missing module', async () => {
await assert.rejects(esmock('./main.js', {
'node:process': {
default: {
cwd: () => 'here',
env: {
key1: 'env1',
key2: 'env2',
}
}
}
}), 'import should fail due to missing module')
})
describe('when esmock succeeds anyways', () => {
let main
beforeEach(async () => {
main = await esmock('./main.js', {
'node:process': {
default: {
cwd: () => 'here',
env: {
key1: 'env1',
key2: 'env2',
}
}
}
})
})
it('should mock exports defined in opts', async () => {
assert.equal(main.getEnv('key1'), 'env1')
assert.equal(main.getEnv('key2'), 'env2')
assert.equal(main.getCwd(), 'here')
})
it('should not define any exports for missing module (1)', async () => {
assert.throws(main.getExtra, 'getExtra should throw error')
})
it('should not define any exports for missing module (2)', async () => {
assert.throws(main.getDefault, 'getDefault should throw error')
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment