Skip to content

Commit

Permalink
Merge branch '4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
sgravrock committed Jan 1, 2022
2 parents 30843ab + 73344ee commit cff54c2
Show file tree
Hide file tree
Showing 40 changed files with 723 additions and 872 deletions.
15 changes: 0 additions & 15 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,10 @@ executors:
docker:
- image: circleci/node:12
working_directory: ~/workspace
node12_0:
docker:
- image: circleci/node:12.0
working_directory: ~/workspace
node12_17:
docker:
- image: circleci/node:12.17
working_directory: ~/workspace
node12_16:
docker:
- image: circleci/node:12.16
working_directory: ~/workspace
node10:
docker:
- image: circleci/node:10
working_directory: ~/workspace

jobs:
test:
Expand Down Expand Up @@ -75,9 +63,6 @@ workflows:
- node14_7
- node12_latest
- node12_17 # first with dynamic import() of commonjs modules
- node12_16
- node12_0
- node10
cron:
<<: *push_workflow
triggers:
Expand Down
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
spec/fixtures/cjs-syntax-error/syntax_error.js
spec/fixtures/esm-importing-commonjs-syntax-error/syntax_error.js
spec/fixtures/js-loader-import/*.js
spec/fixtures/js-loader-default/*.js
spec/fixtures/esm-reporter-packagejson/customReporter.js
91 changes: 22 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,90 +14,43 @@ This module allows you to run Jasmine specs for your Node.js code. The output wi

## Documentation

https://jasmine.github.io/edge/node.html
https://jasmine.github.io/setup/nodejs.html

## Installation
```sh
# Local installation:
npm install --save-dev jasmine

# Global installation
npm install -g jasmine
```

## Initializing

To initialize a project for Jasmine

`jasmine init`

To initialize a project for Jasmine when being installed locally

`node_modules/.bin/jasmine init`

or

`npx jasmine init`

To seed your project with some examples

`jasmine examples`

## Usage
## Quick Start

To run your test suite
Installation:

`jasmine`

## Configuration

Customize `spec/support/jasmine.json` to enumerate the source and spec files you would like the Jasmine runner to include.
You may use dir glob strings.
More information on the format of `jasmine.json` can be found in [the documentation](http://jasmine.github.io/edge/node.html#section-Configuration)

Alternatively, you may specify the path to your `jasmine.json` by setting an environment variable or an option:

```shell
jasmine JASMINE_CONFIG_PATH=relative/path/to/your/jasmine.json
jasmine --config=relative/path/to/your/jasmine.json
```sh
npm install --save-dev jasmine
```

## Using ES modules

If the name of a spec file or helper file ends in `.mjs`, Jasmine will load it
as an [ES module](https://nodejs.org/docs/latest-v13.x/api/esm.html) rather
than a CommonJS module. This allows the spec file or helper to import other
ES modules. No extra configuration is required.

You can also use ES modules with names ending in `.js` by adding
`"jsLoader": "import"` to `jasmine.json`. This should work for CommonJS modules
as well as ES modules. We expect to make it the default in a future release.
Please [log an issue](https://github.com/jasmine/jasmine-npm/issues) if you have
code that doesn't load correctly with `"jsLoader": "import"`.
To initialize a project for Jasmine:

```sh
npx jasmine init
````

# Filtering specs
To seed your project with some examples:

Execute only those specs which filename match given glob:
```sh
npx jasmine examples
````
```shell
jasmine "spec/**/critical/*Spec.js"
```
To run your test suite:
Or a single file:
```sh
npx jasmine
````

```shell
jasmine spec/currentSpec.js
```
## ES and CommonJS module compatibility

Or execute only those specs which name matches a particular regex:
Jasmine is compatible with both ES modules and CommonJS modules. See the
[setup guide](https://jasmine.github.io/setup/nodejs.html) for more information.

```shell
jasmine --filter "adapter21*"
```

(where the *name* of a spec is the first parameter passed to `describe()`)
## Node version compatibility

Jasmine supports Node 12.x where x >=17, Node 14, and Node 16.

## Support

Expand Down
91 changes: 74 additions & 17 deletions lib/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const path = require('path');
const fs = require('fs');
const Loader = require('./loader');

exports = module.exports = Command;

Expand Down Expand Up @@ -30,7 +31,7 @@ function Command(projectBaseDir, examplesDir, print) {

const command = this;

this.run = function(jasmine, commands) {
this.run = async function(jasmine, commands) {
setEnvironmentVariables(commands);

let commandToRun;
Expand All @@ -53,7 +54,7 @@ function Command(projectBaseDir, examplesDir, print) {
print('');
help({print: print});
} else {
runJasmine(jasmine, env, print);
await runJasmine(jasmine, env, print);
}
}
};
Expand All @@ -72,7 +73,6 @@ function parseOptions(argv) {
reporter,
configPath,
filter,
stopOnFailure,
failFast,
random,
seed;
Expand All @@ -89,10 +89,8 @@ function parseOptions(argv) {
helpers.push(arg.match("^--helper=(.*)")[1]);
} else if (arg.match("^--require=")) {
requires.push(arg.match("^--require=(.*)")[1]);
} else if (arg.match("^--stop-on-failure=")) {
stopOnFailure = arg.match("^--stop-on-failure=(.*)")[1] === 'true';
} else if (arg.match("^--fail-fast=")) {
failFast = arg.match("^--fail-fast=(.*)")[1] === 'true';
} else if (arg === '--fail-fast') {
failFast = true;
} else if (arg.match("^--random=")) {
random = arg.match("^--random=(.*)")[1] === 'true';
} else if (arg.match("^--seed=")) {
Expand All @@ -113,7 +111,6 @@ function parseOptions(argv) {
color: color,
configPath: configPath,
filter: filter,
stopOnFailure: stopOnFailure,
failFast: failFast,
helpers: helpers,
requires: requires,
Expand All @@ -125,14 +122,75 @@ function parseOptions(argv) {
};
}

function runJasmine(jasmine, env, print) {
const loadConfig = require('./loadConfig');
loadConfig(jasmine, env, print);
jasmine.execute(env.files, env.filter)
.catch(function(error) {
console.error(error);
process.exit(1);
async function runJasmine(jasmine, env, print) {
await jasmine.loadConfigFile(env.configPath || process.env.JASMINE_CONFIG_PATH);

if (env.failFast !== undefined) {
jasmine.env.configure({
stopSpecOnExpectationFailure: env.failFast,
stopOnSpecFailure: env.failFast
});
}

if (env.seed !== undefined) {
jasmine.seed(env.seed);
}

if (env.random !== undefined) {
jasmine.randomizeTests(env.random);
}

if (env.helpers !== undefined && env.helpers.length) {
jasmine.addHelperFiles(env.helpers);
}

if (env.requires !== undefined && env.requires.length) {
jasmine.addRequires(env.requires);
}

if (env.reporter !== undefined) {
await registerReporter(env.reporter, jasmine);
}

jasmine.showColors(env.color);

try {
await jasmine.execute(env.files, env.filter);
} catch (error) {
console.error(error);
process.exit(1);
}
}

async function registerReporter(reporterModuleName, jasmine) {
let Reporter;

try {
Reporter = await new Loader().load(resolveReporter(reporterModuleName));
} catch (e) {
throw new Error('Failed to load reporter module '+ reporterModuleName +
'\nUnderlying error: ' + e.stack + '\n(end underlying error)');
}

let reporter;

try {
reporter = new Reporter();
} catch (e) {
throw new Error('Failed to instantiate reporter from '+ reporterModuleName +
'\nUnderlying error: ' + e.stack + '\n(end underlying error)');

}
jasmine.clearReporters();
jasmine.addReporter(reporter);
}

function resolveReporter(nameOrPath) {
if (nameOrPath.startsWith('./') || nameOrPath.startsWith('../')) {
return path.resolve(nameOrPath);
} else {
return nameOrPath;
}
}

function initJasmine(options) {
Expand Down Expand Up @@ -199,8 +257,7 @@ function help(options) {
print('%s\tfilter specs to run only those that match the given string', lPad('--filter=', 18));
print('%s\tload helper files that match the given string', lPad('--helper=', 18));
print('%s\tload module that match the given string', lPad('--require=', 18));
print('%s\t[true|false] stop spec execution on expectation failure', lPad('--stop-on-failure=', 18));
print('%s\t[true|false] stop Jasmine execution on spec failure', lPad('--fail-fast=', 18));
print('%s\tstop Jasmine execution on spec failure', lPad('--fail-fast', 18));
print('%s\tpath to your optional jasmine.json', lPad('--config=', 18));
print('%s\tpath to reporter to use instead of the default Jasmine reporter', lPad('--reporter=', 18));
print('%s\tmarker to signal the end of options meant for Jasmine', lPad('--', 18));
Expand Down
6 changes: 4 additions & 2 deletions lib/examples/jasmine.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"helpers": [
"helpers/**/*.?(m)js"
],
"stopSpecOnExpectationFailure": false,
"random": true
"env": {
"stopSpecOnExpectationFailure": false,
"random": true
}
}
15 changes: 15 additions & 0 deletions lib/exit_handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ExitHandler {
constructor(onExit) {
this._onExit = onExit;
}

install() {
process.on('exit', this._onExit);
}

uninstall() {
process.removeListener('exit', this._onExit);
}
}

module.exports = ExitHandler;
Loading

0 comments on commit cff54c2

Please sign in to comment.