Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cspotcode committed May 2, 2020
1 parent a46625b commit 3ea1097
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 28 deletions.
36 changes: 14 additions & 22 deletions dist-raw/node-esm-resolve-implementation.js
Expand Up @@ -292,7 +292,7 @@ function legacyMainResolve(packageJSONUrl, packageConfig) {

function resolveExtensionsWithTryExactName(search) {
if (fileExists(search)) return search;
const resolvedReplacementExtension = getReplacementExtensionCandidates(search);
const resolvedReplacementExtension = resolveReplacementExtensions(search);
if(resolvedReplacementExtension) return resolvedReplacementExtension;
return resolveExtensions(search);
}
Expand All @@ -314,30 +314,20 @@ function resolveExtensions(search) {
return undefined;
}

function resolveReplacementExtensionsWithTryExactName(search) {
if (fileExists(search)) return search;
return getReplacementExtensionCandidates(search);
}

/**
* TS's resolver can resolve foo.js to foo.ts, by replacing .js extension with several source extensions.
* IMPORTANT: preserve ordering according to preferTsExts; this affects resolution behavior!
*/
const replacementExtensions = extensions.filter(ext => ['.js', '.jsx', '.ts', '.tsx'].includes(ext));

function getReplacementExtensionCandidates(search) {
function resolveReplacementExtensions(search) {
if (search.pathname.match(/\.js$/)) {
const pathnameWithoutExtension = search.pathname.slice(0, search.pathname.length - 3);
return replacementExtensions.map(new URL(`${pathnameWithoutExtension}${extension}`, search));
}
return [search];
}

// TODO re-merge with above function
function resolveCandidates(guesses) {
for (let i = 0; i < guesses.length; i++) {
const guess = guesses[i];
if (fileExists(guess)) return guess;
for (let i = 0; i < replacementExtensions.length; i++) {
const extension = replacementExtensions[i];
const guess = new URL(`${pathnameWithoutExtension}${extension}`, search);
if (fileExists(guess)) return guess;
}
}
return undefined;
}
Expand All @@ -361,12 +351,14 @@ function finalizeResolution(resolved, base) {
}

if (StringPrototypeEndsWith(resolved.pathname, '/')) return resolved;
const path = fileURLToPath(resolved);

const file = resolveCandidates(getReplacementExtensionCandidates(resolved));
if (!file) {
throw new ERR_MODULE_NOT_FOUND(
path || resolved.pathname, fileURLToPath(base), 'module');
const file = resolveReplacementExtensions(resolved) || resolved;

const path = fileURLToPath(file);

if (!tryStatSync(path).isFile()) {
throw new ERR_MODULE_NOT_FOUND(
path || resolved.pathname, fileURLToPath(base), 'module');
}

return file;
Expand Down
13 changes: 10 additions & 3 deletions src/index.spec.ts
Expand Up @@ -638,11 +638,18 @@ describe('ts-node', function () {
describe('esm', () => {
this.slow(1000)

// `ts-node/` prefix required to import from ourselves
const cmd = `node --loader ts-node/esm`
const cmd = `node --loader ../../esm.mjs`

it('should compile and execute as ESM', (done) => {
exec(`${cmd} index.ts`, {cwd: join(__dirname, '../tests/esm')}, function (err, stdout) {
exec(`${cmd} index.ts`, { cwd: join(__dirname, '../tests/esm') }, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('foo bar baz\n')

return done()
})
})
it('supports --experimental-specifier-resolution=node', (done) => {
exec(`${cmd} --experimental-specifier-resolution=node index.ts`, { cwd: join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('foo bar baz\n')

Expand Down
3 changes: 3 additions & 0 deletions tests/esm-node-resolver/bar/index.ts
@@ -0,0 +1,3 @@
export const bar = 'bar' as const

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
3 changes: 3 additions & 0 deletions tests/esm-node-resolver/baz.js
@@ -0,0 +1,3 @@
export const baz = 'baz'

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
8 changes: 8 additions & 0 deletions tests/esm-node-resolver/biff.jsx
@@ -0,0 +1,8 @@
export const biff = 'biff'

const React = {
createElement() {}
}
const div = <div></div>

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
3 changes: 3 additions & 0 deletions tests/esm-node-resolver/foo.ts
@@ -0,0 +1,3 @@
export const foo = 'foo' as const

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
8 changes: 8 additions & 0 deletions tests/esm-node-resolver/index.ts
@@ -0,0 +1,8 @@
import {foo} from './foo'
import {bar} from './bar'
import {baz} from './baz'
import {biff} from './biff'

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

console.log(`${foo} ${bar} ${baz} ${biff}`)
3 changes: 3 additions & 0 deletions tests/esm-node-resolver/package.json
@@ -0,0 +1,3 @@
{
"type": "module"
}
8 changes: 8 additions & 0 deletions tests/esm-node-resolver/tsconfig.json
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "ESNext",
"allowJs": true,
"jsx": "react",
"moduleResolution": "node"
}
}
2 changes: 1 addition & 1 deletion tests/esm/baz.js
@@ -1,3 +1,3 @@
export const baz = 'baz' as const
export const baz = 'baz'

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
8 changes: 8 additions & 0 deletions tests/esm/biff.jsx
@@ -0,0 +1,8 @@
export const biff = 'biff'

const React = {
createElement() {}
}
const div = <div></div>

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
3 changes: 2 additions & 1 deletion tests/esm/index.ts
@@ -1,7 +1,8 @@
import {foo} from './foo.js'
import {bar} from './bar.js'
import {baz} from './baz.js'
import {biff} from './biff.js'

if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')

console.log(`${foo} ${bar} ${baz}`)
console.log(`${foo} ${bar} ${baz} ${biff}`)
3 changes: 2 additions & 1 deletion tests/esm/tsconfig.json
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "ESNext",
"allowJs": true
"allowJs": true,
"jsx": "react"
}
}

0 comments on commit 3ea1097

Please sign in to comment.