├── .editorconfig
├── .eslintignore
├── .eslintrc.js
├── .gitattributes
├── .github
├── CODEOWNERS
├── CONTRIBUTING.md
├── FUNDING.yml
├── ISSUE_TEMPLATE.md
├── ISSUE_TEMPLATE
│ ├── BUG.md
│ ├── DOCS.md
│ ├── FEATURE.md
│ ├── MODIFICATION.md
│ └── SUPPORT.md
├── PULL_REQUEST_TEMPLATE.md
└── workflows
│ └── nodejs.yml
├── .gitignore
├── .prettierignore
├── .prettierrc.js
├── CHANGELOG.md
├── LICENSE
├── README.md
├── babel.config.js
├── commitlint.config.js
├── husky.config.js
├── jest.config.js
├── lint-staged.config.js
├── package-lock.json
├── package.json
├── src
├── cjs.js
├── index.js
├── options.json
└── start.js
└── test
├── __snapshots__
└── loader.test.js.snap
├── cjs.test.js
├── fixtures
├── module.js
└── test-file.js
├── helpers
├── compile.js
├── getCompiler.js
├── htmlWithScript.js
├── index.js
├── normalizeErrors.js
└── readAsset.js
└── loader.test.js
/.editorconfig:
--------------------------------------------------------------------------------
1 | # editorconfig.org
2 |
3 | [*]
4 | charset = utf-8
5 | indent_style = space
6 | indent_size = 2
7 | end_of_line = lf
8 | insert_final_newline = true
9 | trim_trailing_whitespace = true
10 |
11 | [*.md]
12 | trim_trailing_whitespace = false
--------------------------------------------------------------------------------
/.eslintignore:
--------------------------------------------------------------------------------
1 | /coverage
2 | /dist
3 | /node_modules
4 | /test/fixtures
--------------------------------------------------------------------------------
/.eslintrc.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | extends: ['@webpack-contrib/eslint-config-webpack', 'prettier'],
4 | };
5 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | * text=auto
2 | bin/* eol=lf
3 | yarn.lock -diff
4 | package-lock.json -diff
--------------------------------------------------------------------------------
/.github/CODEOWNERS:
--------------------------------------------------------------------------------
1 | # These are the default owners for everything in
2 | # webpack-contrib
3 | @webpack-contrib/org-maintainers
4 |
5 | # Add repository specific users / groups
6 | # below here for libs that are not maintained by the org.
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing in @webpack-contrib
2 |
3 | We'd always love contributions to further improve the webpack / webpack-contrib ecosystem!
4 | Here are the guidelines we'd like you to follow:
5 |
6 | - [Questions and Problems](#question)
7 | - [Issues and Bugs](#issue)
8 | - [Feature Requests](#feature)
9 | - [Pull Request Submission Guidelines](#submit-pr)
10 | - [Commit Message Conventions](#commit)
11 |
12 | ## Got a Question or Problem?
13 |
14 | Please submit support requests and questions to StackOverflow using the tag [[webpack]](http://stackoverflow.com/tags/webpack).
15 | StackOverflow is better suited for this kind of support though you may also inquire in [Webpack Gitter](https://gitter.im/webpack/webpack).
16 | The issue tracker is for bug reports and feature discussions.
17 |
18 | ## Found an Issue or Bug?
19 |
20 | Before you submit an issue, please search the issue tracker, maybe an issue for your problem already exists and the discussion might inform you of workarounds readily available.
21 |
22 | We want to fix all the issues as soon as possible, but before fixing a bug we need to reproduce and confirm it. In order to reproduce bugs, we ask that you to provide a minimal reproduction scenario (github repo or failing test case). Having a live, reproducible scenario gives us a wealth of important information without going back & forth to you with additional questions like:
23 |
24 | - version of Webpack used
25 | - version of the loader / plugin you are creating a bug report for
26 | - the use-case that fails
27 |
28 | A minimal reproduce scenario allows us to quickly confirm a bug (or point out config problems) as well as confirm that we are fixing the right problem.
29 |
30 | We will be insisting on a minimal reproduce scenario in order to save maintainers time and ultimately be able to fix more bugs. We understand that sometimes it might be hard to extract essentials bits of code from a larger code-base but we really need to isolate the problem before we can fix it.
31 |
32 | Unfortunately, we are not able to investigate / fix bugs without a minimal reproduction, so if we don't hear back from you we are going to close an issue that doesn't have enough info to be reproduced.
33 |
34 | ## Feature Requests?
35 |
36 | You can _request_ a new feature by creating an issue on Github.
37 |
38 | If you would like to _implement_ a new feature, please submit an issue with a proposal for your work `first`, to be sure that particular makes sense for the project.
39 |
40 | ## Pull Request Submission Guidelines
41 |
42 | Before you submit your Pull Request (PR) consider the following guidelines:
43 |
44 | - Search Github for an open or closed PR that relates to your submission. You don't want to duplicate effort.
45 | - Commit your changes using a descriptive commit message that follows our [commit message conventions](#commit). Adherence to these conventions is necessary because release notes are automatically generated from these messages.
46 | - Fill out our `Pull Request Template`. Your pull request will not be considered if it is ignored.
47 | - Please sign the `Contributor License Agreement (CLA)` when a pull request is opened. We cannot accept your pull request without this. Make sure you sign with the primary email address associated with your local / github account.
48 |
49 | ## Webpack Contrib Commit Conventions
50 |
51 | Each commit message consists of a **header**, a **body** and a **footer**. The header has a special
52 | format that includes a **type**, a **scope** and a **subject**:
53 |
54 | ```
55 | ():
56 |
57 |
58 |
59 |
60 | ```
61 |
62 | The **header** is mandatory and the **scope** of the header is optional.
63 |
64 | Any line of the commit message cannot be longer 100 characters! This allows the message to be easier
65 | to read on GitHub as well as in various git tools.
66 |
67 | The footer should contain a [closing reference to an issue](https://help.github.com/articles/closing-issues-via-commit-messages/) if any.
68 |
69 | Examples:
70 |
71 | ```
72 | docs(readme): update install instructions
73 | ```
74 |
75 | ```
76 | fix: refer to the `entrypoint` instead of the first `module`
77 | ```
78 |
79 | ### Revert
80 |
81 | If the commit reverts a previous commit, it should begin with `revert:`, followed by the header of the reverted commit.
82 | In the body it should say: `This reverts commit .`, where the hash is the SHA of the commit being reverted.
83 |
84 | ### Type
85 |
86 | Must be one of the following:
87 |
88 | - **build**: Changes that affect the build system or external dependencies (example scopes: babel, npm)
89 | - **chore**: Changes that fall outside of build / docs that do not effect source code (example scopes: package, defaults)
90 | - **ci**: Changes to our CI configuration files and scripts (example scopes: circleci, travis)
91 | - **docs**: Documentation only changes (example scopes: readme, changelog)
92 | - **feat**: A new feature
93 | - **fix**: A bug fix
94 | - **perf**: A code change that improves performance
95 | - **refactor**: A code change that neither fixes a bug nor adds a feature
96 | - **revert**: Used when reverting a committed change
97 | - **style**: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons)
98 | - **test**: Addition of or updates to Jest tests
99 |
100 | ### Scope
101 |
102 | The scope is subjective & depends on the `type` see above. A good example would be a change to a particular class / module.
103 |
104 | ### Subject
105 |
106 | The subject contains a succinct description of the change:
107 |
108 | - use the imperative, present tense: "change" not "changed" nor "changes"
109 | - don't capitalize the first letter
110 | - no dot (.) at the end
111 |
112 | ### Body
113 |
114 | Just as in the **subject**, use the imperative, present tense: "change" not "changed" nor "changes".
115 | The body should include the motivation for the change and contrast this with previous behavior.
116 |
117 | ### Footer
118 |
119 | The footer should contain any information about **Breaking Changes** and is also the place to
120 | reference GitHub issues that this commit **Closes**.
121 |
122 | **Breaking Changes** should start with the word `BREAKING CHANGE:` with a space or two newlines. The rest of the commit message is then used for this.
123 |
124 | Example
125 |
126 | ```
127 | BREAKING CHANGE: Updates to `Chunk.mapModules`.
128 |
129 | This release is not backwards compatible with `Webpack 2.x` due to breaking changes in webpack/webpack#4764
130 | Migration: see webpack/webpack#5225
131 |
132 | ```
133 |
134 | ## Testing Your Pull Request
135 |
136 | You may have the need to test your changes in a real-world project or dependent
137 | module. Thankfully, Github provides a means to do this. Add a dependency to the
138 | `package.json` for such a project as follows:
139 |
140 | ```json
141 | {
142 | "devDependencies": {
143 | "mocha-loader": "webpack-contrib/mocha-loader#{id}/head"
144 | }
145 | }
146 | ```
147 |
148 | Where `{id}` is the # ID of your Pull Request.
149 |
150 | ## Contributor License Agreement
151 |
152 | When submitting your contribution, a CLA (Contributor License Agreement) bot will come by to verify that you signed the [CLA](https://cla.js.foundation/webpack-contrib/mocha-loader).
153 | If it is your first time, it will link you to the right place to sign it.
154 | However, if you have committed your contributions using an email that is not the same as your email used on GitHub, the CLA bot can't accept your contribution.
155 |
156 | Run `git config user.email` to see your Git email, and verify it with [your GitHub email](https://github.com/settings/emails).
157 |
158 | ## Thanks
159 |
160 | For your interest, time, understanding, and for following this simple guide.
161 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | open_collective: webpack
2 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
17 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/BUG.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: 🐛 Bug Report
3 | about: Something went awry and you'd like to tell us about it.
4 | ---
5 |
6 |
16 |
17 | - Operating System:
18 | - Node Version:
19 | - NPM Version:
20 | - webpack Version:
21 | - mocha-loader Version:
22 |
23 | ### Expected Behavior
24 |
25 |
26 |
27 | ### Actual Behavior
28 |
29 |
30 |
31 | ### Code
32 |
33 | ```js
34 | // webpack.config.js
35 | // If your code blocks are over 20 lines, please paste a link to a gist
36 | // (https://gist.github.com).
37 | ```
38 |
39 | ```js
40 | // additional code, HEY YO remove this block if you don't need it
41 | ```
42 |
43 | ### How Do We Reproduce?
44 |
45 |
51 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/DOCS.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: 📚 Documentation
3 | about: Are the docs lacking or missing something? Do they need some new 🔥 hotness? Tell us here.
4 | ---
5 |
6 |
16 |
17 | Documentation Is:
18 |
19 |
20 |
21 | - [ ] Missing
22 | - [ ] Needed
23 | - [ ] Confusing
24 | - [ ] Not Sure?
25 |
26 | ### Please Explain in Detail...
27 |
28 | ### Your Proposal for Changes
29 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/FEATURE.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: ✨ Feature Request
3 | about: Suggest an idea for this project
4 | ---
5 |
6 |
16 |
17 | - Operating System:
18 | - Node Version:
19 | - NPM Version:
20 | - webpack Version:
21 | - mocha-loader Version:
22 |
23 | ### Feature Proposal
24 |
25 | ### Feature Use Case
26 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/MODIFICATION.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: 🔧 Modification Request
3 | about: Would you like something work differently? Have an alternative approach? This is the template for you.
4 | ---
5 |
6 |
16 |
17 | - Operating System:
18 | - Node Version:
19 | - NPM Version:
20 | - webpack Version:
21 | - mocha-loader Version:
22 |
23 | ### Expected Behavior / Situation
24 |
25 | ### Actual Behavior / Situation
26 |
27 | ### Modification Proposal
28 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/SUPPORT.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: 🆘 Support, Help, and Advice
3 | about: 👉🏽 Need support, help, or advice? Don't open an issue! Head to StackOverflow or https://gitter.im/webpack/webpack.
4 | ---
5 |
6 | Hey there! If you need support, help, or advice then this is not the place to ask.
7 | Please visit [StackOverflow](https://stackoverflow.com/questions/tagged/webpack)
8 | or [the Webpack Gitter](https://gitter.im/webpack/webpack) instead.
9 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 |
10 |
11 | This PR contains a:
12 |
13 | - [ ] **bugfix**
14 | - [ ] new **feature**
15 | - [ ] **code refactor**
16 | - [ ] **test update**
17 | - [ ] **typo fix**
18 | - [ ] **metadata update**
19 |
20 | ### Motivation / Use-Case
21 |
22 |
27 |
28 | ### Breaking Changes
29 |
30 |
34 |
35 | ### Additional Info
36 |
--------------------------------------------------------------------------------
/.github/workflows/nodejs.yml:
--------------------------------------------------------------------------------
1 | name: mocha-loader
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 | - next
8 | pull_request:
9 | branches:
10 | - master
11 | - next
12 |
13 | jobs:
14 | lint:
15 | name: Lint - ${{ matrix.os }} - Node v${{ matrix.node-version }}
16 |
17 | env:
18 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19 |
20 | strategy:
21 | matrix:
22 | os: [ubuntu-latest]
23 | node-version: [12.x]
24 |
25 | runs-on: ${{ matrix.os }}
26 |
27 | steps:
28 | - uses: actions/checkout@v2
29 | with:
30 | fetch-depth: 0
31 |
32 | - name: Use Node.js ${{ env.node-version }}
33 | uses: actions/setup-node@v1
34 | with:
35 | node-version: ${{ env.node-version }}
36 |
37 | - name: Use latest NPM
38 | run: sudo npm i -g npm
39 |
40 | - name: Install dependencies
41 | run: npm ci
42 |
43 | - name: Lint
44 | run: npm run lint
45 |
46 | # - name: Security audit
47 | # run: npm run security
48 |
49 | - name: Check commit message
50 | uses: wagoid/commitlint-github-action@v1
51 |
52 | test:
53 | name: Test - ${{ matrix.os }} - Node v${{ matrix.node-version }}, Webpack ${{ matrix.webpack-version }}
54 |
55 | strategy:
56 | matrix:
57 | os: [ubuntu-latest, windows-latest, macos-latest]
58 | node-version: [10.x, 12.x, 14.x]
59 | webpack-version: [4, latest]
60 |
61 | runs-on: ${{ matrix.os }}
62 |
63 | steps:
64 | - name: Setup Git
65 | if: matrix.os == 'windows-latest'
66 | run: git config --global core.autocrlf input
67 |
68 | - uses: actions/checkout@v2
69 |
70 | - name: Use Node.js ${{ matrix.node-version }}
71 | uses: actions/setup-node@v1
72 | with:
73 | node-version: ${{ matrix.node-version }}
74 |
75 | - name: Use latest NPM on ubuntu/macos
76 | if: matrix.os == 'ubuntu-latest' || matrix.os == 'macos-latest'
77 | run: sudo npm i -g npm
78 |
79 | - name: Use latest NPM on windows
80 | if: matrix.os == 'windows-latest'
81 | run: npm i -g npm
82 |
83 | - name: Install dependencies
84 | run: npm ci
85 |
86 | - name: Install webpack ${{ matrix.webpack-version }}
87 | run: npm i webpack@${{ matrix.webpack-version }}
88 |
89 | - name: Run tests for webpack version ${{ matrix.webpack-version }}
90 | run: npm run test:coverage -- --ci
91 |
92 | - name: Submit coverage data to codecov
93 | uses: codecov/codecov-action@v1
94 | with:
95 | token: ${{ secrets.CODECOV_TOKEN }}
96 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | logs
2 | *.log
3 | npm-debug.log*
4 | .eslintcache
5 | /coverage
6 | /dist
7 | /local
8 | /reports
9 | /node_modules
10 | .DS_Store
11 | Thumbs.db
12 | .idea
13 | *.iml
14 | .vscode
15 | *.sublime-project
16 | *.sublime-workspace
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | /coverage
2 | /dist
3 | /node_modules
4 | /test/fixtures
5 | CHANGELOG.md
--------------------------------------------------------------------------------
/.prettierrc.js:
--------------------------------------------------------------------------------
1 | module.exports = { singleQuote: true };
2 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4 |
5 | ### [5.1.5](https://github.com/webpack-contrib/mocha-loader/compare/v5.1.4...v5.1.5) (2020-10-15)
6 |
7 | * update `css-loader`
8 |
9 | ### [5.1.4](https://github.com/webpack-contrib/mocha-loader/compare/v5.1.3...v5.1.4) (2020-10-10)
10 |
11 | * update `style-loader`
12 |
13 | ### [5.1.3](https://github.com/webpack-contrib/mocha-loader/compare/v5.1.2...v5.1.3) (2020-10-09)
14 |
15 | * update `schema-utils`
16 |
17 | ### [5.1.2](https://github.com/webpack-contrib/mocha-loader/compare/v5.1.1...v5.1.2) (2020-07-31)
18 |
19 |
20 | ### Bug Fixes
21 |
22 | * compat with mocha@8.1.0 ([#104](https://github.com/webpack-contrib/mocha-loader/issues/104)) ([1a4a1d6](https://github.com/webpack-contrib/mocha-loader/commit/1a4a1d69e37a70da7bc17a62310a8def442eedde))
23 |
24 | ### [5.1.1](https://github.com/webpack-contrib/mocha-loader/compare/v5.1.0...v5.1.1) (2020-07-27)
25 |
26 |
27 | ### Chore
28 |
29 | * updating dependencies
30 |
31 | ## [5.1.0](https://github.com/webpack-contrib/mocha-loader/compare/v5.0.0...v5.1.0) (2020-06-11)
32 |
33 |
34 | ### Features
35 |
36 | * support mocha@8 ([#96](https://github.com/webpack-contrib/mocha-loader/issues/96)) ([82f82a6](https://github.com/webpack-contrib/mocha-loader/commit/82f82a6084e355a86844ffe8222100c41c73e2ec))
37 |
38 | ### [5.0.0](https://github.com/webpack-contrib/mocha-loader/compare/v4.0.2...v5.0.0) (2020-03-24)
39 |
40 |
41 | ### Bug Fixes
42 |
43 | * remove broken handling of "target": "node" ([#92](https://github.com/webpack-contrib/mocha-loader/issues/92)) ([b84eb10](https://github.com/webpack-contrib/mocha-loader/commit/b84eb10b700b331815d1ca4d62ef61d269994eb9))
44 |
45 | ### BREAKING CHANGES
46 |
47 | * the `node` target doesn't supported
48 |
49 |
50 |
51 | ### [4.0.2](https://github.com/webpack-contrib/mocha-loader/compare/v4.0.1...v4.0.2) (2020-03-19)
52 |
53 | * update deps
54 |
55 | ### [4.0.1](https://github.com/webpack-contrib/mocha-loader/compare/v4.0.0...v4.0.1) (2020-01-09)
56 |
57 |
58 | ### Bug Fixes
59 |
60 | * compatibility with mocha@7 ([#87](https://github.com/webpack-contrib/mocha-loader/issues/87)) ([1d99129](https://github.com/webpack-contrib/mocha-loader/commit/1d99129a1d097bce38384e6db236a13faee0b815))
61 | * remove archived script-loader ([#86](https://github.com/webpack-contrib/mocha-loader/issues/86)) ([8c86110](https://github.com/webpack-contrib/mocha-loader/commit/8c861101a8acb9be935bd218870a9f7ee742b045))
62 |
63 | # [4.0.0](https://github.com/webpack-contrib/mocha-loader/compare/v3.0.0...v4.0.0) (2019-12-02)
64 |
65 |
66 | ### Fixes
67 |
68 | * compatibility with `webpack@5`
69 |
70 | ### BREAKING CHANGES
71 |
72 | * minimum required nodejs version is `10.13`
73 | * minimum required mocha version is `5`
74 |
75 |
76 |
77 | # [3.0.0](https://github.com/webpack-contrib/mocha-loader/compare/v2.0.1...v3.0.0) (2019-08-15)
78 |
79 |
80 | ### Features
81 |
82 | * improve validation errors
83 |
84 |
85 | ### BREAKING CHANGES
86 |
87 | * drop support for Node < 8.9.0
88 | * minimum supported `webpack` version is `4`
89 |
90 |
91 |
92 |
93 | # [2.0.0](https://github.com/webpack-contrib/mocha-loader/compare/v1.1.3...v2.0.0) (2018-08-27)
94 |
95 |
96 | ### Chores
97 |
98 | * **package:** upgrade to `css-loader@1.0.0` and `style-loader@0.22.1` (`dependencies`) ([#75](https://github.com/webpack-contrib/mocha-loader/issues/75)) ([5ab9ea1](https://github.com/webpack-contrib/mocha-loader/commit/5ab9ea1))
99 |
100 |
101 | ### BREAKING CHANGES
102 |
103 | * **package:** requires `node >= v6.0.0`
104 |
105 |
106 |
107 |
108 | ## [1.1.3](https://github.com/webpack-contrib/mocha-loader/compare/v1.1.2...v1.1.3) (2018-01-24)
109 |
110 |
111 |
112 |
113 | ## [1.1.2](https://github.com/webpack-contrib/mocha-loader/compare/v1.1.1...v1.1.2) (2018-01-05)
114 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright JS Foundation and other contributors
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | 'Software'), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DEPRECATED
2 |
3 | If you would like to continue to support/maintenance this package please contact me https://twitter.com/alexander_akait
4 |
5 |
13 |
14 | [![npm][npm]][npm-url]
15 | [![node][node]][node-url]
16 | [![deps][deps]][deps-url]
17 | [![tests][tests]][tests-url]
18 | [![coverage][cover]][cover-url]
19 | [![chat][chat]][chat-url]
20 | [![size][size]][size-url]
21 |
22 | # mocha-loader
23 |
24 | Allows [Mocha](https://mochajs.org/) tests to be loaded and run via webpack.
25 |
26 | ## Getting Started
27 |
28 | To begin, you'll need to install `mocha-loader` and `mocha`:
29 |
30 | ```console
31 | npm install --save-dev mocha-loader mocha
32 | ```
33 |
34 | Then add the plugin to your `webpack` config. For example:
35 |
36 | **file.js**
37 |
38 | ```js
39 | import test from './test.js';
40 | ```
41 |
42 | **webpack.config.js**
43 |
44 | ```js
45 | module.exports = {
46 | entry: './entry.js',
47 | output: {
48 | path: __dirname,
49 | filename: 'bundle.js',
50 | },
51 | module: {
52 | rules: [
53 | {
54 | test: /test\.js$/,
55 | use: 'mocha-loader',
56 | exclude: /node_modules/,
57 | },
58 | ],
59 | },
60 | };
61 | ```
62 |
63 | And run `webpack` via your preferred method.
64 |
65 | Alternative usage (without configuration):
66 |
67 | ```js
68 | import test from 'mocha-loader!./test.js';
69 | ```
70 |
71 | No options for loader.
72 |
73 | ## Examples
74 |
75 | ### Basic
76 |
77 | **file.js**
78 |
79 | ```js
80 | module.exports = true;
81 | ```
82 |
83 | **test.js**
84 |
85 | ```js
86 | describe('Test', () => {
87 | it('should succeed', (done) => {
88 | setTimeout(done, 1000);
89 | });
90 |
91 | it('should fail', () => {
92 | setTimeout(() => {
93 | throw new Error('Failed');
94 | }, 1000);
95 | });
96 |
97 | it('should randomly fail', () => {
98 | if (require('./module')) {
99 | throw new Error('Randomly failed');
100 | }
101 | });
102 | });
103 | ```
104 |
105 | ## Contributing
106 |
107 | Please take a moment to read our contributing guidelines if you haven't yet done so.
108 |
109 | [CONTRIBUTING](./.github/CONTRIBUTING.md)
110 |
111 | ## License
112 |
113 | [MIT](./LICENSE)
114 |
115 | [npm]: https://img.shields.io/npm/v/mocha-loader.svg
116 | [npm-url]: https://npmjs.com/package/mocha-loader
117 | [node]: https://img.shields.io/node/v/mocha-loader.svg
118 | [node-url]: https://nodejs.org
119 | [deps]: https://david-dm.org/webpack-contrib/mocha-loader.svg
120 | [deps-url]: https://david-dm.org/webpack-contrib/mocha-loader
121 | [tests]: https://github.com/webpack-contrib/mocha-loader/workflows/mocha-loader/badge.svg
122 | [tests-url]: https://github.com/webpack-contrib/mocha-loader/actions
123 | [cover]: https://codecov.io/gh/webpack-contrib/mocha-loader/branch/master/graph/badge.svg
124 | [cover-url]: https://codecov.io/gh/webpack-contrib/mocha-loader
125 | [chat]: https://badges.gitter.im/webpack/webpack.svg
126 | [chat-url]: https://gitter.im/webpack/webpack
127 | [size]: https://packagephobia.now.sh/badge?p=mocha-loader
128 | [size-url]: https://packagephobia.now.sh/result?p=mocha-loader
129 |
--------------------------------------------------------------------------------
/babel.config.js:
--------------------------------------------------------------------------------
1 | const MIN_BABEL_VERSION = 7;
2 |
3 | module.exports = (api) => {
4 | api.assertVersion(MIN_BABEL_VERSION);
5 | api.cache(true);
6 |
7 | return {
8 | presets: [
9 | [
10 | '@babel/preset-env',
11 | {
12 | targets: {
13 | node: '10.13.0',
14 | },
15 | },
16 | ],
17 | ],
18 | };
19 | };
20 |
--------------------------------------------------------------------------------
/commitlint.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | extends: ['@commitlint/config-conventional'],
3 | };
4 |
--------------------------------------------------------------------------------
/husky.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | hooks: {
3 | 'pre-commit': 'lint-staged',
4 | 'commit-msg': 'commitlint -E HUSKY_GIT_PARAMS',
5 | },
6 | };
7 |
--------------------------------------------------------------------------------
/jest.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | testEnvironment: 'node',
3 | };
4 |
--------------------------------------------------------------------------------
/lint-staged.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | '*.js': ['prettier --write', 'eslint --fix'],
3 | '*.{json,md,yml,css,ts}': ['prettier --write'],
4 | };
5 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mocha-loader",
3 | "version": "5.1.5",
4 | "description": "Mocha loader for webpack",
5 | "license": "MIT",
6 | "repository": "webpack-contrib/mocha-loader",
7 | "author": "Tobias Koppers @sokra",
8 | "homepage": "https://github.com/webpack-contrib/mocha-loader",
9 | "bugs": "https://github.com/webpack-contrib/mocha-loader/issues",
10 | "funding": {
11 | "type": "opencollective",
12 | "url": "https://opencollective.com/webpack"
13 | },
14 | "main": "dist/cjs.js",
15 | "engines": {
16 | "node": ">= 10.13.0"
17 | },
18 | "scripts": {
19 | "start": "npm run build -- -w",
20 | "clean": "del-cli dist",
21 | "prebuild": "npm run clean",
22 | "build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
23 | "commitlint": "commitlint --from=master",
24 | "security": "npm audit",
25 | "lint:prettier": "prettier --list-different .",
26 | "lint:js": "eslint --cache .",
27 | "lint": "npm-run-all -l -p \"lint:**\"",
28 | "test:only": "cross-env NODE_ENV=test jest",
29 | "test:watch": "npm run test:only -- --watch",
30 | "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage",
31 | "pretest": "npm run lint",
32 | "test": "npm run test:coverage",
33 | "prepare": "npm run build",
34 | "release": "standard-version",
35 | "defaults": "webpack-defaults"
36 | },
37 | "files": [
38 | "dist"
39 | ],
40 | "peerDependencies": {
41 | "mocha": "^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0",
42 | "webpack": "^4.0.0 || ^5.0.0"
43 | },
44 | "dependencies": {
45 | "css-loader": "^5.0.0",
46 | "loader-utils": "^2.0.0",
47 | "schema-utils": "^3.0.0",
48 | "style-loader": "^2.0.0"
49 | },
50 | "devDependencies": {
51 | "@babel/cli": "^7.11.6",
52 | "@babel/core": "^7.11.6",
53 | "@babel/preset-env": "^7.11.5",
54 | "@commitlint/cli": "^11.0.0",
55 | "@commitlint/config-conventional": "^11.0.0",
56 | "@webpack-contrib/defaults": "^6.3.0",
57 | "@webpack-contrib/eslint-config-webpack": "^3.0.0",
58 | "babel-jest": "^26.5.2",
59 | "cross-env": "^7.0.2",
60 | "del-cli": "^3.0.1",
61 | "eslint": "^7.11.0",
62 | "eslint-config-prettier": "^6.12.0",
63 | "eslint-plugin-import": "^2.22.1",
64 | "express": "^4.17.1",
65 | "husky": "^4.3.0",
66 | "jest": "^26.5.3",
67 | "lint-staged": "^10.4.0",
68 | "memfs": "^3.2.0",
69 | "mocha": "^8.1.3",
70 | "npm-run-all": "^4.1.5",
71 | "prettier": "^2.1.2",
72 | "puppeteer": "^5.3.1",
73 | "standard-version": "^9.0.0",
74 | "webpack": "^4.44.2"
75 | },
76 | "keywords": [
77 | "webpack",
78 | "mocha"
79 | ]
80 | }
81 |
--------------------------------------------------------------------------------
/src/cjs.js:
--------------------------------------------------------------------------------
1 | const loader = require('./index');
2 |
3 | module.exports = loader.default;
4 |
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import { getOptions } from 'loader-utils';
2 | import { validate } from 'schema-utils';
3 |
4 | import schema from './options.json';
5 |
6 | const startScriptPath = require.resolve('./start.js');
7 | const mochaJs = require.resolve('mocha/mocha.js');
8 | const mochaCss = require.resolve('mocha/mocha.css');
9 | const { stringify } = JSON;
10 |
11 | export default function mochaLoader(source) {
12 | return source;
13 | }
14 |
15 | mochaLoader.pitch = pitch;
16 |
17 | export function pitch(req) {
18 | const options = getOptions(this) || {};
19 |
20 | validate(schema, options, {
21 | name: 'Mocha Loader',
22 | baseDataPath: 'options',
23 | });
24 |
25 | options.ui = options.ui || 'bdd';
26 |
27 | const source = [];
28 | if (this.target === 'web' || this.target === 'electron-renderer') {
29 | source.push(
30 | `require(${stringify(`!!style-loader!css-loader!${mochaCss}`)});`
31 | );
32 | source.push(`var mochaModule = require(${stringify(`!!${mochaJs}`)});`);
33 | source.push(`var mochaInstance = window.mocha || mochaModule;`);
34 | source.push(`mochaInstance.setup(${stringify(options)});`);
35 | source.push(`require(${stringify(`!!${req}`)});`);
36 | source.push(`require(${stringify(`!!${startScriptPath}`)});`);
37 | source.push('if(module.hot) {');
38 | source.push('\tmodule.hot.accept();');
39 | source.push('\tmodule.hot.dispose(function() {');
40 | source.push('\t\tmochaInstance.suite.suites.length = 0;');
41 | source.push("\t\tvar stats = document.getElementById('mocha-stats');");
42 | source.push("\t\tvar report = document.getElementById('mocha-report');");
43 | source.push('\t\tstats && stats.parentNode.removeChild(stats);');
44 | source.push('\t\treport && report.parentNode.removeChild(report);');
45 | source.push('\t});');
46 | source.push('}');
47 | } else {
48 | throw new Error(`Unsupported target environment ${this.target}`);
49 | }
50 |
51 | return source.join('\n');
52 | }
53 |
--------------------------------------------------------------------------------
/src/options.json:
--------------------------------------------------------------------------------
1 | {
2 | "type": "object",
3 | "properties": {
4 | "allowUncaught": {
5 | "type": "boolean"
6 | },
7 | "asyncOnly": {
8 | "type": "boolean"
9 | },
10 | "bail": {
11 | "type": "boolean"
12 | },
13 | "checkLeaks": {
14 | "type": "boolean"
15 | },
16 | "delay": {
17 | "type": "boolean"
18 | },
19 | "enableTimeouts": {
20 | "type": "boolean"
21 | },
22 | "fgrep": {
23 | "type": "string"
24 | },
25 | "forbidOnly": {
26 | "type": "boolean"
27 | },
28 | "forbidPending": {
29 | "type": "boolean"
30 | },
31 | "fullStackTrace": {
32 | "type": "boolean"
33 | },
34 | "global": {
35 | "type": "array",
36 | "items": {
37 | "type": "string"
38 | }
39 | },
40 | "grep": {
41 | "type": "string"
42 | },
43 | "growl": {
44 | "type": "boolean"
45 | },
46 | "hideDiff": {
47 | "type": "boolean"
48 | },
49 | "ignoreLeaks": {
50 | "type": "boolean"
51 | },
52 | "invert": {
53 | "type": "boolean"
54 | },
55 | "noHighlighting": {
56 | "type": "boolean"
57 | },
58 | "reporter": {
59 | "type": "string"
60 | },
61 | "reporterOption": {
62 | "type": "object"
63 | },
64 | "retries": {
65 | "type": "number"
66 | },
67 | "slow": {
68 | "type": "number"
69 | },
70 | "timeout": {
71 | "anyOf": [
72 | {
73 | "type": "string"
74 | },
75 | {
76 | "type": "number"
77 | }
78 | ]
79 | },
80 | "ui": {
81 | "type": "string"
82 | },
83 | "color": {
84 | "type": "boolean"
85 | },
86 | "useInlineDiffs": {
87 | "type": "boolean"
88 | }
89 | },
90 | "additionalProperties": false
91 | }
92 |
--------------------------------------------------------------------------------
/src/start.js:
--------------------------------------------------------------------------------
1 | /* eslint-disable no-var */
2 |
3 | var mochaModule = require('mocha/mocha.js');
4 |
5 | var mochaInstance = window.mocha || mochaModule;
6 |
7 | /* global window document */
8 | window.addEventListener('DOMContentLoaded', function runMocha() {
9 | if (!document.getElementById('mocha')) {
10 | // eslint-disable-next-line
11 | var mochaContainer = document.createElement('div');
12 | mochaContainer.id = 'mocha';
13 | document.body.appendChild(mochaContainer);
14 | }
15 | mochaInstance.run();
16 | });
17 |
--------------------------------------------------------------------------------
/test/__snapshots__/loader.test.js.snap:
--------------------------------------------------------------------------------
1 | // Jest Snapshot v1, https://goo.gl/fbAQLP
2 |
3 | exports[`mocha-loader executes mocha tests when evaluating bundle in browser: errors 1`] = `Array []`;
4 |
5 | exports[`mocha-loader executes mocha tests when evaluating bundle in browser: warnings 1`] = `Array []`;
6 |
--------------------------------------------------------------------------------
/test/cjs.test.js:
--------------------------------------------------------------------------------
1 | import src from '../src';
2 | import cjs from '../src/cjs';
3 |
4 | describe('cjs', () => {
5 | it('should exported', () => {
6 | expect(cjs).toEqual(src);
7 | });
8 |
9 | it('should export "pitch" function', () => {
10 | expect(cjs.pitch).toBeInstanceOf(Function);
11 | });
12 | });
13 |
--------------------------------------------------------------------------------
/test/fixtures/module.js:
--------------------------------------------------------------------------------
1 | export default true;
2 |
--------------------------------------------------------------------------------
/test/fixtures/test-file.js:
--------------------------------------------------------------------------------
1 | import test from './module';
2 |
3 | describe('behavior', () => {
4 | it('should pass', () => {
5 | if (!test) {
6 | throw new Error(`should not happen`)
7 | }
8 | });
9 |
10 | it('should fail', () => {
11 | if (test) {
12 | throw new Error(`always fails`)
13 | }
14 | });
15 | });
16 |
--------------------------------------------------------------------------------
/test/helpers/compile.js:
--------------------------------------------------------------------------------
1 | export default (compiler) => {
2 | return new Promise((resolve, reject) => {
3 | compiler.run((error, stats) => {
4 | if (error) {
5 | return reject(error);
6 | }
7 |
8 | return resolve(stats);
9 | });
10 | });
11 | };
12 |
--------------------------------------------------------------------------------
/test/helpers/getCompiler.js:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 |
3 | import webpack from 'webpack';
4 | import { createFsFromVolume, Volume } from 'memfs';
5 |
6 | export default (entry, config = {}) => {
7 | const fullConfig = {
8 | mode: 'development',
9 | devtool: config.devtool || false,
10 | context: path.join(__dirname, '../fixtures'),
11 | entry,
12 | output: {
13 | path: path.join(__dirname, '../outputs'),
14 | filename: '[name].bundle.js',
15 | chunkFilename: '[name].chunk.js',
16 | },
17 | ...config,
18 | };
19 |
20 | const compiler = webpack(fullConfig);
21 |
22 | if (!config.outputFileSystem) {
23 | const outputFileSystem = createFsFromVolume(new Volume());
24 | // Todo remove when we drop webpack@4 support
25 | outputFileSystem.join = path.join.bind(path);
26 |
27 | compiler.outputFileSystem = outputFileSystem;
28 | }
29 |
30 | return compiler;
31 | };
32 |
--------------------------------------------------------------------------------
/test/helpers/htmlWithScript.js:
--------------------------------------------------------------------------------
1 | export default (bundleFileName) => `
2 |
3 |
4 |
5 |
6 |
7 | test with mocha loader
8 |
9 |
10 |
11 |
12 |
13 | `;
14 |
--------------------------------------------------------------------------------
/test/helpers/index.js:
--------------------------------------------------------------------------------
1 | import compile from './compile';
2 | import getCompiler from './getCompiler';
3 | import htmlWithScript from './htmlWithScript';
4 | import normalizeErrors from './normalizeErrors';
5 | import readAsset from './readAsset';
6 |
7 | export { compile, getCompiler, normalizeErrors, readAsset, htmlWithScript };
8 |
--------------------------------------------------------------------------------
/test/helpers/normalizeErrors.js:
--------------------------------------------------------------------------------
1 | function removeCWD(str) {
2 | const isWin = process.platform === 'win32';
3 | let cwd = process.cwd();
4 |
5 | if (isWin) {
6 | // eslint-disable-next-line no-param-reassign
7 | str = str.replace(/\\/g, '/');
8 | // eslint-disable-next-line no-param-reassign
9 | cwd = cwd.replace(/\\/g, '/');
10 | }
11 |
12 | return str.replace(new RegExp(cwd, 'g'), '');
13 | }
14 |
15 | export default (errors) =>
16 | errors.map((error) =>
17 | removeCWD(error.toString().split('\n').slice(0, 2).join('\n'))
18 | );
19 |
--------------------------------------------------------------------------------
/test/helpers/readAsset.js:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 |
3 | export default function readAsset(
4 | asset,
5 | { outputFileSystem },
6 | { compilation: { outputOptions } }
7 | ) {
8 | const { path: outputPath } = outputOptions;
9 | try {
10 | return outputFileSystem.readFileSync(path.join(outputPath, asset), 'utf8');
11 | } catch (error) {
12 | return error.toString();
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/test/loader.test.js:
--------------------------------------------------------------------------------
1 | import path from 'path';
2 | import { promisify } from 'util';
3 | import { once } from 'events';
4 |
5 | import puppeteer from 'puppeteer';
6 | import express from 'express';
7 |
8 | import {
9 | compile,
10 | getCompiler,
11 | normalizeErrors,
12 | readAsset,
13 | htmlWithScript,
14 | } from './helpers';
15 |
16 | const loaderPath = path.join(__dirname, '../src');
17 | const testFileFixturePath = require.resolve('./fixtures/test-file.js');
18 | const getElementText = (el) =>
19 | el.getProperty('textContent').then((handle) => handle.jsonValue());
20 |
21 | describe('mocha-loader', () => {
22 | const disposables = new Set();
23 |
24 | afterEach(async () => {
25 | for (const dispose of Array.from(disposables).reverse()) {
26 | // eslint-disable-next-line no-await-in-loop
27 | await dispose();
28 | }
29 | disposables.clear();
30 | });
31 |
32 | it('executes mocha tests when evaluating bundle in browser', async () => {
33 | // bundle using loader
34 | const compiler = getCompiler(`${loaderPath}!${testFileFixturePath}`);
35 | if (compiler.inputFileSystem.purge) {
36 | disposables.add(() => compiler.inputFileSystem.purge());
37 | }
38 | const stats = await compile(compiler);
39 |
40 | const { errors, warnings } = stats.compilation;
41 |
42 | expect(normalizeErrors(warnings)).toMatchSnapshot('warnings');
43 | expect(normalizeErrors(errors)).toMatchSnapshot('errors');
44 |
45 | // serve bundle result
46 | const bundleFileName = 'main.bundle.js';
47 | const outputBundleText = readAsset(bundleFileName, compiler, stats);
48 | const app = express();
49 | app.get('/', (_req, res) => res.end(htmlWithScript(bundleFileName)));
50 | app.get(`/${bundleFileName}`, (_req, res) => res.end(outputBundleText));
51 |
52 | const httpServer = app.listen(3000);
53 | await once(httpServer, 'listening');
54 | disposables.add(promisify(httpServer.close.bind(httpServer)));
55 |
56 | // start browser and open test page
57 | const browser = await puppeteer.launch({
58 | devtools: false,
59 | timeout: 15000,
60 | });
61 | disposables.add(() => browser.close());
62 |
63 | const [page] = await browser.pages();
64 | const pageErrors = [];
65 | page.on('pageerror', (e) => pageErrors.push(e));
66 |
67 | await page.goto('http://localhost:3000/');
68 | expect(pageErrors).toHaveLength(0);
69 |
70 | const passesEl = await page.waitForSelector('#mocha-stats .passes');
71 | const passes = await getElementText(passesEl);
72 |
73 | const failuresEl = await page.waitForSelector('#mocha-stats .failures');
74 | const failed = await getElementText(failuresEl);
75 |
76 | expect(passes).toContain('passes: 1');
77 | expect(failed).toContain('failures: 1');
78 | }, 30000);
79 | });
80 |
--------------------------------------------------------------------------------