Skip to content

Commit

Permalink
Merge pull request #26 from larsthorup/feature/default-bare-path
Browse files Browse the repository at this point in the history
enable bare path by default
  • Loading branch information
larsthorup authored Oct 26, 2021
2 parents 5ee20ec + 232ad7e commit 38cb0d8
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 71 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

## Upgrade from 1 to 2

Explicitly specify `--enableBarePath false` on command line or `"enableBarePath": false` in config file.

Alternatively you should ensure that your `test.html` file does not use inline script of type "module".
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,30 @@ You can optionally specify a JSON file with reporter options:
$ npx mocha-vite-puppeteer --reporter mocha-junit-reporter --reporter-options mocha-junit-reporter.config.json
```

By default `mocha-vite-puppeteer` will use a default "test.html" to configure mocha and load your test-files. But you can also write your own `test.html` placed next to `index.html`. Here is an example:
By default `mocha-vite-puppeteer` will create a default "test.html" to configure mocha and load your test-files. But you can also write your own `test.html` placed next to `index.html`. Here is an example:

```html
<!DOCTYPE html>
<html lang="en">
<body>
<script type="module">
import "mocha";
mocha.setup({ ui: "bdd" });
</script>
<script type="module">
const modules = import.meta.globEager("/src/**/*.test.{js,jsx}");
</script>
<script type="module" src="/mocha-setup.js"></script>
<script type="module" src="/test-loader.js"></script>
</body>
</html>
```

mocha-setup.js:

```js
import "mocha";
mocha.setup({ ui: "bdd" });
```

test-loader.js:

```js
const modules = import.meta.globEager("/src/**/*.test.{js,jsx}");
```

## Available Flags

Expand All @@ -106,8 +113,8 @@ By default `mocha-vite-puppeteer` will use a default "test.html" to configure mo
| --verbose | -v | false | Enables verbose reporting from Mocha-Vite-Puppeteer. Useful for debugging these flags and inputs. |
| --debug | -d | false | Sets debug mode for Mocha-Vite-Puppeteer. Automatically disabled puppeteer headless mode. |
| --config | -c | undefined | Advanced config options. See section below for details |
| --enableBarePath | | | Load entry html file from "/" (html file cannot use inline script of type "module") |
| --coverage | | | Instrument and collect code coverage during test. Use "nyc" for reporting |
| --enableBarePath | | true | Load entry html file from "/" (html file cannot use inline script of type "module") |
| --coverage | | false | Instrument and collect code coverage during test. Use "nyc" for reporting |

<details>
<summary>Advanced Configuration</summary>
Expand Down
74 changes: 29 additions & 45 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,14 @@ if (verbose) { console.log('Starting mocha-vite-puppeteer with options: ', JSON.
const root = '.'; // Note: relative to cwd
const entry = options.entry; // Note: relative to root
const testHtmlAbsolutePath = path.resolve(path.join(root, entry));
const useDefaultTestHtml = entry === 'test.html' && !fs.existsSync(testHtmlAbsolutePath);
const barePathEnabled = useDefaultTestHtml || options.enableBarePath;
const entryPath = barePathEnabled ? '/' : `/${entry}`;
const reporter = options.reporter;
const reporterOptions = options.reporterOptions ? JSON.parse(fs.readFileSync(options.reporterOptions, 'utf-8')) : undefined;
const debug = options.debug;
const mochaProtocolPrefix = 'mocha$protocol:';
// ----

// Note: https://mochajs.org/#running-mocha-in-the-browser
const require = module.createRequire(import.meta.url);
const mochaAbsolutePath = require.resolve('mocha/mocha.js');
const readTestHtml = () => {
if (useDefaultTestHtml) {
return `
const mochaSetupAbsolutePath = path.resolve(path.join(root, 'mocha-setup.js'));
const testLoaderAbsolutePath = path.resolve(path.join(root, 'test-loader.js'));
const createDefaultFiles = entry === 'test.html'
&& !fs.existsSync(testHtmlAbsolutePath)
&& !fs.existsSync(mochaSetupAbsolutePath)
&& !fs.existsSync(testLoaderAbsolutePath);
if (createDefaultFiles) {
fs.writeFileSync(testHtmlAbsolutePath, `
<!DOCTYPE html>
<html lang="en">
<body>
Expand All @@ -53,16 +46,28 @@ const readTestHtml = () => {
env: {}
};
</script>
<script type="module" src="/default-mocha-setup.js"></script>
<script type="module" src="/default-test-loader.js"></script>
<script type="module" src="/mocha-setup.js"></script>
<script type="module" src="/test-loader.js"></script>
</body>
</html>
`;
} else {
return fs.readFileSync(testHtmlAbsolutePath, 'utf-8');
}
};
const testHtml = readTestHtml();
</html>`);
fs.writeFileSync(mochaSetupAbsolutePath, `
import 'mocha';
mocha.setup({ ui: 'bdd' });`);
fs.writeFileSync(testLoaderAbsolutePath, `
const modules = import.meta.globEager('/src/**/*.test.{js,jsx,ts,tsx}');`);
}
const barePathEnabled = options.enableBarePath;
const entryPath = barePathEnabled ? '/' : `/${entry}`;
const reporter = options.reporter;
const reporterOptions = options.reporterOptions ? JSON.parse(fs.readFileSync(options.reporterOptions, 'utf-8')) : undefined;
const debug = options.debug;
const mochaProtocolPrefix = 'mocha$protocol:';
// ----

// Note: https://mochajs.org/#running-mocha-in-the-browser
const require = module.createRequire(import.meta.url);
const mochaAbsolutePath = require.resolve('mocha/mocha.js');
const testHtml = fs.readFileSync(testHtmlAbsolutePath, 'utf-8');

const testHtmlPlugin = {
name: 'testHtmlPlugin',
Expand All @@ -72,27 +77,6 @@ const testHtmlPlugin = {
return testHtml;
},
},
resolveId(id) {
switch (id) {
case '/default-mocha-setup.js':
return '@default-mocha-setup';
case '/default-test-loader.js':
return '@default-test-loader';
}
},
load(id) {
switch (id) {
case '@default-mocha-setup':
return `
import 'mocha';
mocha.setup({ ui: 'bdd' });
`;
case '@default-test-loader':
return `
const modules = import.meta.globEager('/src/**/*.test.{js,jsx,ts,tsx}');
`;
}
},
};

const istanbulPlugin = IstanbulPlugin(options.istanbul || {});
Expand Down
11 changes: 7 additions & 4 deletions optionsParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cliOptions = getopt({
verbose: { key: 'v', description: 'Verbose Output' },
debug: { key: 'd', description: 'Enable debug mode. Note: test will run until quit via console (ctrl+c)' },
config: { key: 'c', description: 'Relative path to JSON config file. See project description for more details.', args: 1, default: undefined },
enableBarePath: { description: 'Load entry html file from "/" (html file cannot use inline script of type "module")' },
enableBarePath: { description: 'Load entry html file from "/" (html file cannot use inline script of type "module")', args: 1, default: undefined },
coverage: { description: 'Instrument and collect code coverage during test. Use "nyc" for reporting' }
});

Expand All @@ -21,10 +21,13 @@ if (cliOptions.args) {
console.warn('Mocha-vite-puppeteer recieved args that don\'t match the supported syntax. Please check the github for syntax help if this was a mistake')
};

const definedProps = obj => Object.fromEntries(
Object.entries(obj).filter(([k, v]) => v !== undefined)
const definedProps = Object.fromEntries(
Object.entries(cliOptions).filter(([k, v]) => v !== undefined)
);
if (definedProps.enableBarePath !== undefined) {
definedProps.enableBarePath = definedProps.enableBarePath === 'true';
}

const jsonOptions = cliOptions.config && cliOptions.config !== 'undefined' ? JSON.parse(fs.readFileSync(cliOptions.config, 'utf-8')) : undefined;

export const options = { ...jsonOptions, ...definedProps(cliOptions) };
export const options = { enableBarePath: true, ...jsonOptions, ...definedProps };
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mocha-vite-puppeteer",
"version": "1.12.0",
"version": "2.0.0-beta.1",
"type": "module",
"description": "Run your Mocha tests with Vite bundler and Puppeteer",
"bin": {
Expand Down
5 changes: 3 additions & 2 deletions test/app/config/mocha-vite-puppeteer.config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"port": 3010
}
"port": 3010,
"enableBarePath": false
}
1 change: 0 additions & 1 deletion test/app/coverage/mocha-vite-puppeteer.config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"enableBarePath": true,
"coverage": true,
"istanbul": {
"include": ["src/*"]
Expand Down
4 changes: 4 additions & 0 deletions test/app/default-test-html/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Note: these files are generated by default when missing and should thus not be in git
test.html
mocha-setup.js
test-loader.js
10 changes: 5 additions & 5 deletions test/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
"build": "vite build",
"serve": "vite preview",
"test": "npm run test:default-config && npm run test:flags && npm run test:config && npm run test:output && npm run test:bare && npm run test:coverage && npm run test:default-test-html",
"test:bare": "cd bare && node ../../../cli --enableBarePath",
"test:bare": "cd bare && node ../../../cli",
"test:config": "cd config && node ../../../cli -c mocha-vite-puppeteer.config.json",
"test:coverage": "cd coverage && node ../../../cli -c mocha-vite-puppeteer.config.json && nyc report --reporter=text --cwd . && nyc check-coverage --statements 100 --cwd .",
"test:default-config": "cd default-config && node ../../../cli",
"test:default-test-html": "cd default-test-html && node ../../../cli",
"test:flags": "cd flags && node ../../../cli -e test-flags.html -p 3006",
"test:output": "cd default-config && node ../../../cli | grep -q \"{ innerText: 'count is: 1' }\" || (echo Expected output not found && exit 1)"
"test:default-config": "cd default-config && node ../../../cli --enableBarePath false",
"test:default-test-html": "cd default-test-html && rm -f test.html mocha-setup.js test-loader.js && node ../../../cli",
"test:flags": "cd flags && node ../../../cli -e test-flags.html -p 3006 --enableBarePath false",
"test:output": "cd default-config && node ../../../cli --enableBarePath false | grep -q \"{ innerText: 'count is: 1' }\" || (echo Expected output not found && exit 1)"
},
"dependencies": {
"@testing-library/react": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion test/performance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@mocha-vite-puppeteer/test-app",
"version": "0.0.0",
"scripts": {
"test": "node src/generated/generate.mjs && node ../../cli"
"test": "node src/generated/generate.mjs && node ../../cli --enableBarePath false"
},
"dependencies": {
"@testing-library/react": "^12.0.0",
Expand Down

0 comments on commit 38cb0d8

Please sign in to comment.