my-runner
v3.1.3
Published
Run CommonJS script in sandbox with customized style
Downloads
6
Readme
my-runner
Run CommonJS script in sandbox with customized style
How it works?
my-runner uses vm module for mocking an CommonJS's script environment. So we can make the environment as your wish.
For example, jest uses customized environment for running test files. And we could use it running browser's script.
Installation
npm install my-runner
# or use yarn
yarn add my-runner
Usage
Package
const myRunner = require('my-runner')
myRunner.runFile('/path/to/file.js').module.exports
Cli
npm i my-runner -g
my-runner --help
API
run
Run code script
Parameters
code
{string}opts
{{}} (optional, default{}
)opts.rootDir
string? The run environment's root directory path.opts.myrunnerrc
boolean? Whether or not to look up .my-runnerrc or my-runner.config.js file.opts.preset
({} | Function | {name: string, options: any})? A preset that is used as a base for configuration.opts.moduleNameMapper
Object<string, string>? A map from regular expressions to module names that allow to stub out resources, like images or styles with a single module.opts.modulePathIgnorePatterns
Array<string>? An array of regexp pattern strings that are matched against all module paths before those paths are to be considered 'visible' to the module loader.opts.modulePaths
Array<string>? An array of absolute paths to additional locations to search when resolving modules. Use NODE_PATH env variable by default.opts.moduleFileExtensions
Array<string> An array of file extensions your modules use. (optional, default['.js','.json','.jsx','.ts','.tsx','.node']
)opts.moduleDirectories
Array<string> An array of directory names to be searched recursively up from the requiring module's location. (optional, default['node_modules']
)opts.preserveSymlinks
boolean? preserveSymlinksopts.resolver
Function? This option allows the use of a custom resolver. More information see jest.opts.browser
boolean Respect Browserify's "browser" field in package.json when resolving modules. (optional, defaultfalse
)opts.transformIgnorePatterns
Array<string> An array of regexp pattern strings that are matched against all source file paths before transformation. If the test path matches any of the patterns, it will not be transformed. (optional, default['/node_modules/']
)opts.transform
Object<string, (string | Function | {name: string, options: any})> A map from regular expressions to paths to transformers. A transformer is a module that provides a synchronous function for transforming source files. (optional, default{}
)opts.globalSetup
string? This option allows the use of a custom global setup module which exports an async function that is triggered once.opts.globals
Object? A set of global variables that need to be available in environments.opts.transformContext
Function? A sync function for transforming context.opts.global
any The reference on global environment. (optional, default{...global}
)opts.moduleCache
{}? The reference ofrequire.cache
opts.moduleMain
{}? The reference ofrequire.main
opts.fs
Object The fs module. (optional, defaultrequire('fs')
)opts.vm
Object The vm module. (optional, defaultrequire('vm')
)
Returns RunResult
runFile
Run file script
Parameters
Returns RunResult
RunResult
Type: {module: {exports}, exports, require, global}
defaultAdvancedOptions
The default options
How to mock module?
my-runner had injected myRunner
in global.
for example, Mocking prop-types
and setting moduleNameMapper
to be {"^prop-types$": "/path/to/mock-prop-types.js"}
myRunner
requireActual
Requires the actual module rather than the mock module.
- Type:
Function
Contributing
- Fork it!
- Create your new branch:
git checkout -b feature-new
orgit checkout -b fix-which-bug
- Start your magic work now
- Make sure npm test passes
- Commit your changes:
git commit -am 'feat: some description (close #123)'
orgit commit -am 'fix: some description (fix #123)'
- Push to the branch:
git push
- Submit a pull request :)
Authors
This library is written and maintained by imcuttle, [email protected].
License
MIT - imcuttle 🐟