Skip to content

Instantly share code, notes, and snippets.

@mearns
Created April 10, 2019 16:59
Show Gist options
  • Save mearns/312827df5839f882e9e608aeccbede27 to your computer and use it in GitHub Desktop.
Save mearns/312827df5839f882e9e608aeccbede27 to your computer and use it in GitHub Desktop.
Source map support for Babel and Mocha
node_modules/
build/
dist/
node_modules/
build/
dist/

The stuff I've used to transpile an npm project with babel, enabling source map support for dev and unit tests (with mocha).

Running npm run test-only should run your tests with on-the-fly transpiling, but I haven't figured out how to get stack traces to be processed with source maps.

If you want to debug your tests or get stack traces from your tests processed with source maps, then run npm run compile-dev followed by npm run test-dev. The former will transpile your source and test code into the build/ directory, with source maps (this is only needed if your src or test files change), and the latter will run the transpiled tests in that directory.

npm i -ED "@babel/cli" "@babel/core" "@babel/node" "@babel/register" "babel-plugin-node-source-map-support" "source-map-support" "if-env" "mkdirp" "mocha" "npm-run-all" "rimraf"
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": [
"--timeout", "999999", "--colors", "${workspaceFolder}/build/test/**/*.spec.js"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"outFiles": [
"${workspaceFolder}/build/**/*.js"
]
}
]
}
{
"scripts": {
"clean": "rimraf dist/ build/",
"test-dev": "mocha ./build/test/**/*.spec.js",
"test-only": "mocha --require source-map-support/register --require @babel/register ./test/**/*.spec.js",
"test": "nyc npm run test-only",
"compile-dev:app": "mkdirp build/src/ && babel src/ --out-dir build/src/ --plugins node-source-map-support --source-maps true",
"compile-dev:test": "mkdirp build/test/ && babel test/ --out-dir build/test/ --plugins node-source-map-support --source-maps true",
"compile-dev": "npm-run-all compile-dev:*",
"compile-prod:app": "babel src/ --out-dir dist/ --source-maps false",
"compile-prod": "npm-run-all compile-prod:*",
"compile": "if-env NODE_ENV=production && npm run compile-prod || npm run compile-dev"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment