Skip to content

Commit

Permalink
feat!: add support for mocha test runner
Browse files Browse the repository at this point in the history
BREAKING CHANGE: zora and mocha are dev dependencies
  • Loading branch information
acostalima committed Jan 22, 2021
1 parent 4fcccad commit c9424ba
Show file tree
Hide file tree
Showing 36 changed files with 1,626 additions and 598 deletions.
1 change: 0 additions & 1 deletion .eslintcache

This file was deleted.

24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';

const { rules: jestRules } = require('@moxy/eslint-config-jest/lib/rules/jest');

module.exports = {
root: true,
parser: "@babel/eslint-parser",
env: {
node: true,
},
extends: [
"@moxy/eslint-config-base/cjs",
"@moxy/eslint-config-jest"
],
overrides: [
{
files: "fixtures/**",
rules: Object.entries(jestRules).reduce((rules, [key, value]) => ({
...rules,
[key]: 'off',
}), {}),
},
],
};
11 changes: 0 additions & 11 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2020 André Costa Lima <andreclima.pt@gmail.com>
Copyright (c) 2021 André Costa Lima <andreclima.pt@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
34 changes: 22 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,43 @@
$ npm install -D react-native-test-runner
```

## Features

- Supported test runners
- [Zora](https://github.com/lorenzofox3/zora)
- [Mocha](https://github.com/mochajs/mocha/)
- Supported assertion libraries
- [Chai](https://www.chaijs.com/)
- React Native version selection
- iOS runtime and simulator selection
- Android AVD selection

## Limitations

- Support for [Zora](https://github.com/lorenzofox3/zora) test runner only.
- No coverage.
- Only iOS simulators and Android emulators are supported.
- No coverage output.
- No support for Windows and MacOS.
- JavaScriptCore (JSC) engine only on both Android and iOS.
- No TypeScript (TS) support yet.
- Not yet possible to run tests against native modules.
## Usage

```
Usage
$ rn-test [--platform 'ios' | --platform 'android'] [globs ...]
$ rn-test --platform ['ios' | 'android'] [--simulator ios_simulator | --emulator android_avd] [globs ...]
Default glob: **/test?(s)/**/?(*.)+(spec|test).js.
Options
--plaform, -p Platform on which to run the test suites on. Options: 'ios', 'android'.
--simulator, -s iOS simulator to run the test suites on.
--emulator, -e Android emulator or virtual device (AVD) to run the test suites on.
--metroPort, -p Port on which Metro's server should listen to. Default: 8081.
--cwd Current directory. Default: process.cwd().
--rn React Native version to test against. Default: 0.63.4.
--app Absolute path to the test app. Default: ~/.npm/rn-test-app.
--plaform, -p Platform on which to run the test suite on. One of: 'ios', 'android'.
--simulator, -s iOS simulator to run the test suite on.
--emulator, -e Android emulator or virtual device (AVD) to run the test suite on.
--metroPort, -p Port on which Metro's server should listen to. [Default: 8081]
--cwd Current directory. [Default: process.cwd()]
--rn React Native version to use. [Default: 0.63.4]
--runner, -r Test runner to use. One of: 'zora', 'mocha'. [Default: 'zora']
Examples
# Run tests on iPhone 11 simulator with iOS version 14.1
# Run tests on iPhone 11 simulator with iOS version 14.1 runtime
$ rn-test --platform ios --simulator 'iPhone 11 (14.1)' 'test/**/*.test.js'
# Run tests on iPhone 11 simulator with whatever iOS version is available
Expand Down
4 changes: 2 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { filterRNLogs } from './utils';
const App = () => null;

filterRNLogs();
LogBox.ignoreAllLogs();
LogBox?.ignoreAllLogs?.();
AppRegistry.registerComponent('Test', () => App);

const setup = require('./test-runners/zora').default;
const setup = require('runner');

const run = setup();

Expand Down
14 changes: 7 additions & 7 deletions app/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const filterRNLogs = () => {
const filters = ['^Running "Test"'].join('|');
const filters = [
'^Running "Test"',
'Require cycles are allowed, but can result in uninitialized values.',
].join('|');
const filterRegExp = new RegExp(filters);

[
Expand All @@ -8,18 +11,15 @@ const filterRNLogs = () => {
'warn',
'error',
'log',
'group',
'groupCollapsed',
'groupEnd',
'debug',
].forEach((level) => {
const originalFunction = console[level];
const originalFn = console[level];

console[level] = (...args) => {
if (args?.[0].match(filterRegExp)) {
if (args?.[0]?.match(filterRegExp)) {
return;
}
originalFunction.apply(console, args);
originalFn.apply(console, args);
};
});
};
Expand Down
8 changes: 6 additions & 2 deletions cli/bundle-progress/ora.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const ora = require('ora');
class OraBundleProgressBar {
loader = ora();

constructor(testRunner) {
this.testRunner = testRunner;
}

onStart() {
this.loader.start('Bundling JavaScript');
this.loader.start(`Bundling JavaScript to execute tests with ${this.testRunner} test runner`);
}

onProgress(/* { transformedFileCount, totalFileCount } */) {}
Expand All @@ -15,7 +19,7 @@ class OraBundleProgressBar {
this.loader.succeed();
}

onError(error) {
onError({ error }) {
this.loader.fail();
console.error(error);
}
Expand Down
17 changes: 5 additions & 12 deletions cli/test-app.js → cli/create-test-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const semver = require('semver');

const rmdir = promisify(fs.rmdir);

const createTestApp = async ({
module.exports = async ({
reactNativeVersion = '0.63.4',
appPath = path.join(os.homedir(), '.npm', 'rn-test-app'),
} = {}) => {
const loader = ora();

const removeApp = async () => {
const remove = async () => {
loader.start(`Removing test app at ${appPath}`);
try {
await rmdir(appPath, { recursive: true });
Expand All @@ -38,12 +38,12 @@ const createTestApp = async ({
}

if (appPkg && semver.neq(reactNativeVersion, appPkg.dependencies['react-native'])) {
await removeApp();
await remove();
appPkg = null;
}

if (appPkg) {
return ({ removeApp, appPath });
return appPath;
}

try {
Expand All @@ -67,12 +67,5 @@ const createTestApp = async ({
throw error;
}

return ({
removeApp,
appPath,
});
};

module.exports = {
createTestApp,
return appPath;
};
Loading

0 comments on commit c9424ba

Please sign in to comment.