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

Add ESM support #57

Merged
merged 5 commits into from
May 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "0.10"
- "4"
- "6"
- "10"
- "12"
- "13"
- "14"
7 changes: 7 additions & 0 deletions example/esm-node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules/*
!/node_modules/.bin
!/node_modules/teenytest
/node_modules/.bin/*
!/node_modules/.bin/teenytest


21 changes: 21 additions & 0 deletions example/esm-node/lib/dog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var _ = require('lodash')

function Dog (name) {
this.name = name
}

Dog.prototype.bark = function (times) {
return _.map(_.range(times), function (i) {
return 'Woof #' + i
})
}

Dog.prototype.tag = function (side) {
if (side === 'front') {
return 'Hi, I am ' + this.name
} else {
return 'And here is my address'
}
}

module.exports = Dog
1 change: 1 addition & 0 deletions example/esm-node/node_modules/.bin/teenytest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/esm-node/node_modules/teenytest

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions example/esm-node/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "example",
"version": "1.0.0",
"private": true,
"description": "Just an example project",
"directories": {
"test": "test"
},
"scripts": {
"test": "teenytest"
}
}
8 changes: 8 additions & 0 deletions example/esm-node/supports-esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(async function () {
try {
await import('lodash')
process.exit(0)
} catch (err) {
process.exit(1)
}
})()
10 changes: 10 additions & 0 deletions example/esm-node/test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
beforeAll: function () { 'I run absolutely first' },
beforeEach: function () { 'I run before each test' },
afterEach: function () { 'I run right after each test' },
afterAll: function () { 'I run absolutely last' },
options: {
asyncTimeout: 100,
asyncInterval: 1
}
}
24 changes: 24 additions & 0 deletions example/esm-node/test/lib/dog-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var assert = require('core-assert')
var Dog = require('../../lib/dog')

module.exports = {
beforeEach: function () {
this.subject = new Dog('Sam')
},
bark: {
once: function () {
assert.deepEqual(this.subject.bark(1), ['Woof #0'])
},
twice: function () {
assert.deepEqual(this.subject.bark(2), ['Woof #0', 'Woof #1'])
}
},
tag: {
frontSaysName: function () {
assert.equal(this.subject.tag('front'), 'Hi, I am Sam')
},
backSaysAddress: function () {
assert.equal(this.subject.tag('back'), 'And here is my address')
}
}
}
24 changes: 24 additions & 0 deletions example/esm-node/test/lib/dog-test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import assert from 'core-assert'
import Dog from '../../lib/dog.js'

export function beforeEach () {
this.subject = new Dog('Sam')
};

export const bark = {
once: function () {
assert.deepEqual(this.subject.bark(1), ['Woof #0'])
},
twice: function () {
assert.deepEqual(this.subject.bark(2), ['Woof #0', 'Woof #1'])
}
}

export const tag = {
frontSaysName: function () {
assert.equal(this.subject.tag('front'), 'Hi, I am Sam')
},
backSaysAddress: function () {
assert.equal(this.subject.tag('back'), 'And here is my address')
}
}
5 changes: 5 additions & 0 deletions example/esm-node/test/lib/single-function.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var assert = require('core-assert')

module.exports = function blueIsRed () {
assert.equal('blue', 'red')
}
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ var run = require('./lib/run')
module.exports = function (testLocator, userOptions, cb) {
if (arguments.length < 3) { cb = userOptions; userOptions = {} }
var config = configure(testLocator, userOptions)
run(plan(prepare(config)), config, cb)
prepare(config)
.then(function (prepared) {
setImmediate(function () {
run(plan(prepared), config, cb)
})
}, function (er) {
setImmediate(function () {
cb(er)
})
})
}

module.exports.plugins = require('./lib/plugins/store')
12 changes: 6 additions & 6 deletions lib/cli/argv-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ var minimist = require('minimist')
module.exports = function () {
var argv = minimist(process.argv.slice(2))
return {
testLocator: _.isEmpty(argv['_']) ? undefined : _.castArray(argv['_']),
name: argv['name'],
helperPath: argv['helper'],
asyncTimeout: argv['timeout'],
configurator: argv['configurator'],
plugins: argv['plugin'] ? _.castArray(argv['plugin']) : undefined
testLocator: _.isEmpty(argv._) ? undefined : _.castArray(argv._),
name: argv.name,
helperPath: argv.helper,
asyncTimeout: argv.timeout,
configurator: argv.configurator,
plugins: argv.plugin ? _.castArray(argv.plugin) : undefined
}
}
2 changes: 1 addition & 1 deletion lib/configure/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = function () {
return {
cwd: process.cwd(),
output: console.log,
testLocator: 'test/lib/**/*.js',
testLocator: 'test/lib/**/*.{js,mjs}',
name: [],
helperPath: 'test/helper.js',
asyncTimeout: 5000,
Expand Down
15 changes: 8 additions & 7 deletions lib/prepare/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var modules = require('./modules')
var helper = require('./helper')
var loadModules = require('./modules')
var loadHelper = require('./helper')

module.exports = function (config) {
return {
helper: helper(config.helperPath, config.cwd),
modules: modules(config.criteria, config.cwd)
}
module.exports = async function (config) {
const [helper, modules] = await Promise.all([
loadHelper(config.helperPath, config.cwd),
loadModules(config.criteria, config.cwd)
])
return { helper, modules }
}
9 changes: 5 additions & 4 deletions lib/prepare/modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ var filter = require('./filter')
var compact = require('./compact')
var _ = require('lodash')

module.exports = function (criteria, cwd) {
return compact(_.compact(_.flatten(_.map(criteria.testFiles, function (fileCriteria) {
module.exports = async function (criteria, cwd) {
const loadedFiles = await Promise.all(_.map(criteria.testFiles, async function (fileCriteria) {
return filter(
load(fileCriteria.file, cwd),
await load(fileCriteria.file, cwd),
fileCriteria,
cwd
)
}))))
}))
return compact(_.compact(_.flatten(loadedFiles)))
}
21 changes: 17 additions & 4 deletions lib/prepare/modules/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,26 @@ var _ = require('lodash')
function noOpHook () {}
var HOOK_NAMES = ['beforeEach', 'beforeAll', 'afterEach', 'afterAll']

module.exports = function (globPattern, cwd) {
return _.map(glob.sync(globPattern), function (file) {
module.exports = async function (globPattern, cwd) {
return Promise.all(_.map(glob.sync(globPattern), async function (file) {
var testPath = path.resolve(cwd, file)
var testModule = require(testPath)
var testModule
try {
testModule = require(testPath)
} catch (e) {
if (e.code !== 'ERR_REQUIRE_ESM') throw e
try {
testModule = await import(testPath)
} catch (err) {
if (err.message !== 'Not supported') throw err
const message = `You are trying to load test files that are ES modules (${testPath}), but ESM is not supported in this version of Node.js`
console.error(message)
throw new Error(message)
}
}

return buildExampleGroup(testPath, testModule, file, [{ name: 'global' }])
})
}))
}

function buildExampleGroup (name, groupDeclaration, file, ancestors) {
Expand Down
Loading