https://github.com/unional/domture
Create jsdom environment to load NodeJS modules
https://github.com/unional/domture
fixture fixtures jsdom systemjs test test-framework testing testing-tools
Last synced: 5 months ago
JSON representation
Create jsdom environment to load NodeJS modules
- Host: GitHub
- URL: https://github.com/unional/domture
- Owner: unional
- License: mit
- Created: 2017-03-29T05:10:15.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2025-08-21T15:57:02.000Z (10 months ago)
- Last Synced: 2025-10-29T01:36:51.629Z (8 months ago)
- Topics: fixture, fixtures, jsdom, systemjs, test, test-framework, testing, testing-tools
- Language: TypeScript
- Homepage:
- Size: 4.33 MB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# domture
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][travis-image]][travis-url]
[![Codecov][codecov-image]][codecov-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![Greenkeeper][greenkeeper-image]][greenkeeper-url]
[![Semantic Release][semantic-release-image]][semantic-release-url]
[![Visual Studio Code][vscode-image]][vscode-url]
[![Wallaby.js][wallaby-image]][wallaby-url]
`domture` allows you to load packages and scripts directly on a `jsdom` instance for testing purpose.
You can load `npm` packages as well as local files (by default relative to current working directory `.`).
`domture` supports two loaders: `systemjs` and `webpack`.
When using `systemjs`, it leveages `systemjs` magic to load any type of module files.
However, certain NodeJS resolution does not work currently, and also code coverage is not available.
When using `webpack`, it bundles the files you try to import with `webpack` and load them into `jsdom` through script tags.
The NodeJS resolution is complete and code coverage is (will be) available.
However, additional bundle time is needed and bundles management still need to be planned out and implemented in the future.
You also lost the magic from `systemjs`.
By default, `webpack` will be used as the default loader.
For projects using `webpack@4`, please use `domture@2`. For `webpack@3`, please use `domture@1`
## Usage
```ts
import test from 'ava' // or your favorite test runner
import { createDomture } from 'domture'
test('basic usage', async t => {
const domture = await createDomture()
// load package `foo`
const foo = await domture.import('foo')
// load by relative path
const config = await domture.import('./config')
})
test('customize', async t => {
const domture = await createDomture({
// Where to resolve relative path.
rootDir: './lib',
// Preload some scripts ahead of time.
preloadScripts: ['a-package', './someCode.js', './index'],
// Able to load TypeScript code directly
transpiler: 'typescript',
// configure jsdom.
// Can't set `url` and `runScripts`.
// They are used internally.
jsdomConstructorOptions: { ... }
})
})
test('customize with webpack', async t => {
const domture = await createDomture({
/**
* devtool, entry, and output are preserved
*/
webpackConfig: {
// ...
}
})
})
test('customize with systemjs', async t => {
const domture = await createDomture({
loader: 'systemjs',
// Indicates which extension to try during `import()`
// This is needed only if you need to do something special,
// e.g. `allowJs` with TypeScirpt project.
// By default, `domture` will look for 'js' and 'jsx` for JavaScript projects,
// 'ts', 'tsx' for TypeScript projects.
moduleFileExtensions: ['ts', 'js']
systemjsConfig: {
packages: {
// This is need for some packages due to https://github.com/systemjs/systemjs/issues/1603
'make-error': {
main: 'index'
}
},
meta: {
// Do this if `some-global-script.js` is not detected correctly as global script when using `import()`.
'some-global-script.js': {
format: 'global'
}
}
}
})
})
```
## Contribute
```sh
# right after clone
npm install
# begin making changes
git checkout -b
npm run watch
# edit `webpack.config.es5.js` and `rollup.config.es2015.js` to exclude dependencies for the bundle if needed
# after making change(s)
git commit -m ""
git push
# create PR
```
## Npm Commands
There are a few useful commands you can use during development.
```sh
# Run tests (and lint) automatically whenever you save a file.
npm run watch
# Run tests with coverage stats (but won't fail you if coverage does not meet criteria)
npm run test
# Manually verify the project.
# This will be ran during 'npm preversion' so you normally don't need to run this yourself.
npm run verify
# Build the project.
# You normally don't need to do this.
npm run build
# Run tslint
# You normally don't need to do this as `npm run watch` and `npm version` will automatically run lint for you.
npm run lint
```
Generated by [`unional-cli@0.0.0`](https://github.com/unional/unional-cli)
[npm-image]: https://img.shields.io/npm/v/domture.svg?style=flat
[npm-url]: https://npmjs.org/package/domture
[downloads-image]: https://img.shields.io/npm/dm/domture.svg?style=flat
[downloads-url]: https://npmjs.org/package/domture
[travis-image]: https://img.shields.io/travis/unional/domture/master.svg?style=flat
[travis-url]: https://travis-ci.org/unional/domture?branch=master
[codecov-image]: https://codecov.io/gh/unional/satisfier/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/unional/satisfier
[coveralls-image]: https://coveralls.io/repos/github/unional/domture/badge.svg
[coveralls-url]: https://coveralls.io/github/unional/domture
[greenkeeper-image]: https://badges.greenkeeper.io/unional/domture.svg
[greenkeeper-url]: https://greenkeeper.io/
[semantic-release-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
[semantic-release-url]: https://github.com/semantic-release/semantic-release
[wallaby-image]: https://img.shields.io/badge/wallaby.js-configured-green.svg
[wallaby-url]: https://wallabyjs.com
[vscode-image]: https://img.shields.io/badge/vscode-ready-green.svg
[vscode-url]: https://code.visualstudio.com/