Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Running mocha programmatically #89

Closed
eliw00d opened this issue Sep 6, 2017 · 18 comments
Closed

Running mocha programmatically #89

eliw00d opened this issue Sep 6, 2017 · 18 comments
Labels

Comments

@eliw00d
Copy link

eliw00d commented Sep 6, 2017

If mocha is run like this:

mocha -r @std/esm --recursive ./src/test

It results in Unexpected token import errors.

However, if mocha is run like this:

node -r @std/esm ./node_modules/mocha/bin/_mocha --recursive ./src/test

It works as expected, since _mocha is a script and mocha is a wrapper.

So, if I wanted to run mocha programmatically, like this:

./scripts/test.js

let mocha = new Mocha()
mocha.run(failures => process.on('exit', () => process.exit(failures))

And have an npm script like this:

./package.json

"scripts": {
    "test": "node -r @std/esm ./scripts/test"
},

It results in the same Unexpected token import errors as the mocha wrapper.

Any thoughts?

@jdalton jdalton added the bug label Sep 6, 2017
@jdalton
Copy link
Member

jdalton commented Sep 6, 2017

mocha -r @std/esm --recursive ./src/test

It results in Unexpected token import errors.

I'll fix that for the next release.

@jdalton jdalton closed this as completed in 630fa64 Sep 7, 2017
@eliw00d
Copy link
Author

eliw00d commented Sep 7, 2017

So, 0.8.0 fixed the issue using the mocha wrapper, but I am still having issues running mocha programmatically with a Node script. I am importing mocha using import Mocha from 'mocha'. Should I be handling that differently?

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

Programmatic usage would look something like:

require = require("@std/esm")(module)
require("./test-bootstrap.mjs")

The test-bootstrap.mjs file would have your Mocha imports and things.

@eliw00d
Copy link
Author

eliw00d commented Sep 7, 2017

I have the following setup:

./bin/my-scripts.js:

#!/usr/bin/env node
require = require('@std/esm')(module)
require('../scripts/index.js')

./scripts/index.js:

import test from './test'

let script = process.argv.slice(2)[0]

switch (script) {
    case 'test': {
        test()
        break
    }
}

./scripts/test.js:

import Mocha from 'mocha'
import {sync} from 'glob'

export default () => {
    let mocha = new Mocha()

    let testFiles = sync('./src/test/*.js')
    testFiles.forEach(file => mocha.addFile(file))

    mocha.run(failures => process.on('exit', () => process.exit(failures))
}

./package.json:

"bin": {
    "my-scripts": "./bin/my-scripts.js"
},
"scripts": {
    "test": "node ./bin/my-scripts test"
},
"@std/esm": {
    "cjs": true,
    "esm": "js"
}

That way my dependent projects (which also have std/esm with the same options) can do my-scripts test to run tests.

I did notice with 0.8.0 that my dependent projects get a completely unrelated error now (that I could break off into a separate issue) when running my-scripts <script>:

Error [ERR_MISSING_MODULE]: Cannot find module test
    at module.exports.e.a (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:29471)
    at module.exports.e.a (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:19407)
    at /Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:145876
    at Object.<anonymous> (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:145905)
    at e (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:181)
    at /Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:538
    at /Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/esm.js.gz:1:549
    at makeLoaderFunction (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/index.js:31:3)
    at Object.<anonymous> (/Users/eliw00d/Documents/GitHub/project/node_modules/@std/esm/index.js:35:16)
    at Module._compile (module.js:573:30)

Where project is the dependency of the project running the bin script.

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@eliw00d Can you create a test repo for me to look at?

@mAAdhaTTah
Copy link

I'm actually getting this error trying to use esm with ava. I can update my repo to show you.

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@mAAdhaTTah Thanks!

@eliw00d
Copy link
Author

eliw00d commented Sep 7, 2017

@mAAdhaTTah
Copy link

For my example: https://github.com/valtech-nyc/brookjs-cli

Git clone, npm install, and on line 41, change babel-register to @std/esm and run npm test. This was my output:

Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1
(function(require,module,__filename){"use strict";const t=module;module.exports=(function(t){function e(s){if(i[s])return i[s].exports;var n=i[s]={i:s,l:!1,exports:{}};return t[s].call(n.exports,n,n.exports,e),n.l=!0,n.exports}var i={};return e.m=t,e.c=i,e.d=function(t,i,s){e.o(t,i)||Object.defineProperty(t,i,{configurable:!1,enumerable:!0,get:s})},e.n=function(t){var i=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(i,"a",i),i},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=62)})([(function(t,e){t.exports=require("path")}),(function(t,e,i){const s=Object.create;e.a=function(){return s(null)}}),(function(t,e,i){function s(){if(null!==r){const t=r;return r=s.prototype=null,t}return r=s.prototype=new n.a,new s}var n=i(1);let r=null;s(),e.a=s}),(function(t,e,i){e.a=function(t){const e=typeof t;return"function"===e||"object"===e&&null!==t}}),(function(t,e,i){var s=i(1

Error [ERR_MISSING_MODULE]: Cannot find module {"file":"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/src/deltas/__tests__/envDelta.spec.js","baseDir":"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli","tty":{"columns":272,"rows":62},"match":[],"failFast":false,"failWithoutAssertions":true,"serial":false,"require":["/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/index.js"],"cacheEnabled":true,"powerAssert":true,"explicitTitles":false,"babelConfig":"default","resolveTestsFrom":"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli","projectDir":"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli","concurrency":0,"updateSnapshots":false,"color":true,"cacheDir":"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/.cache/ava","precompiled":{"/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/src/deltas/__tests__/envDelta.spec.js":"88d7de6dae49551a35ddfe8ed5a687e7"}}
    at module.exports.e.a (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:29471)
    at module.exports.e.a (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:19407)
    at /Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:145876
    at Object.<anonymous> (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:145905)
    at e (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:181)
    at /Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:538
    at /Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/esm.js.gz:1:549
    at makeLoaderFunction (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/index.js:31:3)
    at Object.<anonymous> (/Users/USNY-JDiGioia/Code/JavaScript/brookjs-cli/node_modules/@std/esm/index.js:35:16)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)

repeated 5 times (once for each test file).

@mAAdhaTTah
Copy link

The inclusion of the JS code at the top seems significant...

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@eliw00d
For your case the dependency test is running until it gets to Mocha which then uses its own mechanism to require modules, so not the loader you created with require("@std/esm")(module). So you're out of luck programmatically loading them unless Mocha adds a way to provide a loader.

The dependent test errors with my-scripts: command not found.

@eliw00d
Copy link
Author

eliw00d commented Sep 7, 2017

@jdalton I updated the repo, but as long as you npm/yarn installed in both dependency and dependent the my-scripts should work.

Regardless, sounds like this might be an issue I should bring up with Mocha, then.

Thanks!

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@mAAdhaTTah Line 41 of which file? Ah the package.json.

@eliw00d
Copy link
Author

eliw00d commented Sep 7, 2017

@jdalton Were you able to get my-scripts to work? That should show the error I was running into with 0.8.0 that was not present with 0.7.1.

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@mAAdhaTTah I found the source of the error (it's likely related to the one @eliw00d is seeing). After I resolve that though @std/esm isn't able to inspect the command-line arguments to find itself being called so it won't activate by way of the ava require option.

jdalton added a commit that referenced this issue Sep 7, 2017
@mAAdhaTTah
Copy link

After I resolve that though @std/esm isn't able to inspect the command-line arguments to find itself being called so it won't activate by way of the ava require option.

So that means it simply wouldn't do anything when run by ava/mocha? I'm not really sure of the underlying mechanism here.

@jdalton
Copy link
Member

jdalton commented Sep 7, 2017

@mAAdhaTTah With the changes to the API in v0.6.0 the loader no longer hooks into the universal module loader mechanism of Node. So folks need to opt-in with the API require = require("@std/esm")(module), or if used by a command-line utility the @std/esm package must be able to inspect the process.argv arguments and find itself in the arguments. When this is masked by configuration then @std/esm can't find itself to know to enable its loader at startup.

Mocha does a nifty trick where it takes its mocha.opts file and inlines them into the process.argv arguments so @std/esm can detect itself. There's a bug open on nyc to add similar functionality. Ava should do this too 😋!

@mAAdhaTTah
Copy link

Sounds good. I'll open an issue there. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

3 participants