Skip to content

Commit

Permalink
Support JS ESM (#9)
Browse files Browse the repository at this point in the history
* Support JS ESM

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

* fixup

Signed-off-by: Matteo Collina <hello@matteocollina.com>

---------

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina authored Jan 30, 2024
1 parent a317d21 commit 60d95a1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
3 changes: 3 additions & 0 deletions fixtures/js-esm/lib/add.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function add (x, y) {
return x + y
}
7 changes: 7 additions & 0 deletions fixtures/js-esm/test/add.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add', () => {
strictEqual(add(1, 2), 3)
})
7 changes: 7 additions & 0 deletions fixtures/js-esm/test/add2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { test } from 'node:test'
import { add } from '../lib/add.js'
import { strictEqual } from 'node:assert'

test('add2', () => {
strictEqual(add(3, 2), 5)
})
15 changes: 11 additions & 4 deletions lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ export default async function runWithTypeScript (config) {
}
config.prefix = prefix
config.setup = (test) => {
for (const chunk of pushable) {
test.reporter.push(chunk)
if (test.reporter) {
for (const chunk of pushable) {
test.reporter.push(chunk)
}
pushable = test.reporter
} else {
for (const chunk of pushable) {
test.push(chunk)
}
pushable = test
}
pushable = test.reporter
}

let tscChild
Expand Down Expand Up @@ -124,7 +131,7 @@ export default async function runWithTypeScript (config) {
} else if (prefix) {
files = await glob(join(prefix, join('**', '*.test.{cjs,mjs,js}')), { ignore, cwd, windowsPathsNoEscape: true })
} else {
files = await glob(join('**', '*.test.{cjs,mjs,js}'), { ignore, cwd, windowsPathsNoEscape: true })
files = await glob(join('**', '*.test.{cjs,mjs,js}'), { ignore, cwd, windowsPathsNoEscape: true, absolute: true })
}

config.files = files
Expand Down
21 changes: 21 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,24 @@ test('only-src', async (t) => {

await completed
})

test('js-esm', async (t) => {
const { strictEqual, completed } = tspl(t, { plan: 2 })
const config = {
files: [],
cwd: join(import.meta.url, '..', 'fixtures', 'js-esm')
}

const stream = await runWithTypeScript(config)

const names = new Set(['add', 'add2'])

stream.on('test:pass', (test) => {
strictEqual(names.has(test.name), true)
names.delete(test.name)
})

stream.resume()

await completed
})

0 comments on commit 60d95a1

Please sign in to comment.