Skip to content

Commit

Permalink
fix: properly handle subdirectories & relative directory tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed May 16, 2023
1 parent 05cefbe commit 4ea4a50
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 17 deletions.
8 changes: 4 additions & 4 deletions lib/wrap-loader.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ wrapLoader.pitch = function pitch (remainingRequest) {

// wrap the test in a function that is only called when we want it called
return `
import { registry } from ${JSON.stringify(`./${path.relative(process.cwd(), path.resolve(argv.outputDir, 'test-registry.js'))}`)}
const { registry } = require('./${path.relative(path.dirname(remainingRequest), path.resolve(argv.outputDir, 'test-registry.cjs')).replace(/\\/g, '/')}')
registry.argv = JSON.parse(${JSON.stringify(JSON.stringify(argv))})
registry.tests.push({
name: JSON.parse('${JSON.stringify(path.normalize(path.relative(process.cwd(), remainingRequest))).replace(/\\/g, '/')}'),
load: () => { return require(${JSON.stringify(`!!${remainingRequest}`)}) }
name: JSON.parse('${JSON.stringify(path.normalize(path.relative(process.cwd(), remainingRequest))).replace(/\\+/g, '/')}'),
load: () => { return require(${JSON.stringify(`!!./${path.basename(remainingRequest)}`)}) }
})
export default {}
module.exports = {}
`
}

Expand Down
2 changes: 1 addition & 1 deletion polendina.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class Polendina {
}

await fs.promises.mkdir(this.outputDir, { recursive: true })
const copyFiles = ['index.html', 'test-registry.js', 'page-run.js', 'common-run.js', this._runnerModule]
const copyFiles = ['index.html', 'test-registry.cjs', 'page-run.js', 'common-run.js', this._runnerModule]
if (this._options.runner.startsWith('bare-')) {
copyFiles.push('bare.js')
}
Expand Down
2 changes: 1 addition & 1 deletion resources/common-run.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* globals WorkerGlobalScope ServiceWorkerGlobalScope clients */

import { registry } from './test-registry.js'
import { registry } from './test-registry.cjs'

const inPage =
typeof window !== 'undefined' &&
Expand Down
4 changes: 2 additions & 2 deletions resources/mocha-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import mochaExport from 'mocha/mocha.js'
import { registry, executionQueue, log, setup } from './common-run.js'

function runMocha () {
async function runMocha () {
// mocha@8 exports what we want, mocha@7 sets a global
const mochaLocal = mochaExport
mochaLocal.setup({ reporter: registry.argv.mochaReporter, ui: 'bdd' })
Expand All @@ -18,7 +18,7 @@ function runMocha () {
mochaLocal.constructor.reporters.Base.consoleLog = log.info

for (const mod of registry.tests) {
mod.load()
await mod.load()
}

let errors = 0
Expand Down
4 changes: 2 additions & 2 deletions resources/tape-run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tape from 'tape'
import { registry, setup, executionQueue } from './common-run.js'
import { Transform } from 'stream'

function runTape () {
async function runTape () {
let failures = 0

const stream = new Transform({
Expand Down Expand Up @@ -32,7 +32,7 @@ function runTape () {
})

for (const mod of registry.tests) {
mod.load()
await mod.load()
}
}

Expand Down
6 changes: 6 additions & 0 deletions resources/test-registry.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// see wrap-loader.cjs

module.exports.registry = {
argv: null,
tests: []
}
6 changes: 0 additions & 6 deletions resources/test-registry.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const __dirname = fileURLToPath(path.dirname(import.meta.url))
const cli = path.join(__dirname, '../polendina-cli.js')

export async function runCli (cwd, args) {
return runCommand(`${cli} test*.js --cleanup ${args || ''}`, cwd)
return runCommand(`${cli} test*.js test/test*.js --cleanup ${args || ''}`, cwd)
}

export async function runCommand (command, cwd) {
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/bare-async-esm/test/test-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { assert } from 'chai'
import index from '../index.js'

export async function test1 () {
console.log('testing bare fixture subdir')
assert.strictEqual(await index(), 'bare test fixture')
}

export async function test2 () {
assert.ok(true, 'yep')
}
11 changes: 11 additions & 0 deletions test/fixtures/bare-async/test/test-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { assert } = require('chai')
const index = require('../')

module.exports.test1 = async function () {
console.log('testing bare fixture subdir')
assert.strictEqual(await index(), 'bare test fixture')
}

module.exports.test2 = async function () {
assert.ok(true, 'yep')
}
7 changes: 7 additions & 0 deletions test/fixtures/bare-sync-esm/test/test-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

import { assert } from 'chai'
import index from '../index.js'

console.log('testing bare fixture subdir')
assert.strictEqual(index, 'bare test fixture')
assert.ok(true, 'yep')
7 changes: 7 additions & 0 deletions test/fixtures/bare-sync/test/test-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

const { assert } = require('chai')
const index = require('../')

console.log('testing bare fixture subdir')
assert.strictEqual(index, 'bare test fixture')
assert.ok(true, 'yep')
4 changes: 4 additions & 0 deletions test/test-bare-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ testing is WORKER
testing bare fixture
✔ test1
✔ test2
test/test-3.js
testing bare fixture subdir
✔ test1
✔ test2
`

it('should run in page', async () => {
Expand Down
2 changes: 2 additions & 0 deletions test/test-bare-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ testing is WORKER
✔ test-1.js
testing bare fixture
✔ test-2.js
testing bare fixture subdir
✔ test/test-3.js
`

it('should run in page', async () => {
Expand Down

0 comments on commit 4ea4a50

Please sign in to comment.