Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking a file with a hashbang using the import key fails #215

Closed
tommy-mitchell opened this issue Jul 27, 2023 · 7 comments
Closed

Mocking a file with a hashbang using the import key fails #215

tommy-mitchell opened this issue Jul 27, 2023 · 7 comments

Comments

@tommy-mitchell
Copy link
Collaborator

tommy-mitchell commented Jul 27, 2023

Given some file cli.js:

#!/usr/bin/env node
import someConstant from "./foo.js";

console.log(someConstant);

Trying to mock this with the import key fails with the error

file:///.../cli.js?esmk=1:1
import {console} from 'file:///import?esmkTreeId=1&esmkModuleId=import&isfound=false&isesm=false&exportNames=console';#!/usr/bin/env node
                                                                                                                      ^

SyntaxError: Invalid or unexpected token
	at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:119:18)
	at ESMLoader.moduleProvider (node:internal/modules/esm/loader:468:14)
	at async link (node:internal/modules/esm/module_job:68:21)

esmock call:

const logs = [];

await esmock("./cli.js", {
	"./foo.js": "bar",
	import: {
		console: { log: (...args) => logs.push(...args) },
	},
});
@tommy-mitchell
Copy link
Collaborator Author

I think the issue is that hashbangs have to be at the start of the file (MDN):

A hashbang comment ... is only valid at the absolute start of a script or module. Note also that no whitespace of any kind is permitted before the #!. The comment consists of all the characters after #! up to the end of the first line; only one such comment is permitted.

esmock is inserting import globals before the hashbang.

@koshic
Copy link
Collaborator

koshic commented Jul 27, 2023

Very similar issue without hashbang.

foo.js

export default 1;

cli.js

import someConstant from "./foo.js";

console.log(someConstant);

1.js

import esmock from "esmock";

await esmock("./cli.js", {
  "./foo.js": "bar",
});
file:///Users/xxxfoo.js?esmkTreeId=1&esmkModuleId=./foo.js&isfound=true&isesm=false&exportNames=0,1,2,default:1
export const 0 = global.esmockCacheGet("file:///Users/xxx/foo.js?esmkTreeId=1&esmkModuleId=./foo.js&isfound=true&isesm=false&exportNames=0,1,2,default").0
             ^

SyntaxError: Unexpected number
    at CustomizedModuleLoader.moduleStrategy (node:internal/modules/esm/translators:116:18)
    at CustomizedModuleLoader.moduleProvider (node:internal/modules/esm/loader:190:14)

@iambumblehead
Copy link
Owner

esmock is inserting import globals before the hashbang.

I think you are correct :) thanks for finding this. I should have time this evening or tomorrow to look at PRs or try to resolve it myself

@iambumblehead
Copy link
Owner

Screen Shot 2023-07-27 at 8 43 19 PM

I don't know why the win32 error is occurring. The error indicates that paths like "node:fs" are only available in esm to packages using "type": "module", but the error originates inside an esm "load" hook and tests are started inside the tests-source-map folder which includes a package.json with "type": "module"

I don't know what causes the issue

@iambumblehead
Copy link
Owner

related nodejs/node#35518

@iambumblehead
Copy link
Owner

The ci error went away when node 18.16 was pinned. The error occurs on the latest 18.17

A new ticket is created at the node repo here let's see what they say nodejs/node#48948

@iambumblehead
Copy link
Owner

closing with this #217

feel free to reopen for any reason

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants