Skip to content

Commit

Permalink
test for PnP resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
koshic committed Oct 14, 2023
1 parent bc4d0a2 commit 12324e5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/local/pnp/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
1 change: 1 addition & 0 deletions tests/local/pnp/disable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
delete process.versions.pnp
14 changes: 14 additions & 0 deletions tests/local/pnp/enable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import module from 'node:module'

async function resolve (specifier, context, next) {
return next(
specifier === 'pnpapi'
? '../tests/local/pnp/api.js'
: specifier, context)
}

module.register && module.register(`
data:text/javascript,
export ${encodeURIComponent(resolve)}`.slice(1))

process.versions.pnp = '3'
35 changes: 35 additions & 0 deletions tests/tests-node/esmock.node.resolver-pnp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import test from 'node:test'
import assert from 'node:assert/strict'
import module from 'node:module'
import { fileURLToPath } from 'node:url'
import resolvewithplus from 'resolvewithplus'
import '../local/pnp/enable.js'
import esmock from 'esmock'
import '../local/pnp/disable.js'
import pnpapi from '../local/pnp/api.js'

const resolver = (id, parent) => {
const url = resolvewithplus(id, parent)
return url !== null ? fileURLToPath(url) : null
}

test.beforeEach(() => {
delete pnpapi.resolveRequest
})

test('should work with pnp resolver', async (t) => {

Check failure on line 20 in tests/tests-node/esmock.node.resolver-pnp.test.js

View workflow job for this annotation

GitHub Actions / build

Unexpected parentheses around single function argument
if (!module.register)
return assert.ok('skip test')

pnpapi.resolveRequest = t.mock.fn(resolver)

const main = await esmock('../local/main.js', {
'../local/mainUtil.js': {
createString: () => 'test string'
}
})

assert.equal(typeof main, 'function')
assert.strictEqual(main(), 'main string, test string')
assert.equal(pnpapi.resolveRequest.mock.callCount(), 2)
})

0 comments on commit 12324e5

Please sign in to comment.