Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(contribute): update writing-a-loader.md #3739

Merged
merged 1 commit into from
May 22, 2020

Conversation

chenxsan
Copy link
Member

The Testing part is kind of outdated and broken. And here's repo I set up in case anyone review it wanna double check https://github.com/chenxsan/testing-loader

@vercel
Copy link

vercel bot commented May 12, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/webpack-docs/webpack-js-org/qrdkdipat
✅ Preview: https://webpack-js-org-git-fork-chenxsan-update-doc.webpack-docs.now.sh

@@ -265,22 +266,24 @@ For instance, the `sass-loader` [specifies `node-sass`](https://github.com/webpa
So you've written a loader, followed the guidelines above, and have it set up to run locally. What's next? Let's go through a simple unit testing example to ensure our loader is working the way we expect. We'll be using the [Jest](https://jestjs.io/) framework to do this. We'll also install `babel-jest` and some presets that will allow us to use the `import` / `export` and `async` / `await`. Let's start by installing and saving these as a `devDependencies`:

``` bash
npm install --save-dev jest babel-jest babel-preset-env
npm install --save-dev jest babel-jest @babel/core @babel/preset-env
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -342,6 +345,7 @@ export default (fixture, options = {}) => {
});

compiler.outputFileSystem = createFsFromVolume(new Volume());
compiler.outputFileSystem.join = path.join.bind(path);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -361,13 +365,16 @@ And now, finally, we can write our test and add an npm script to run it:
__test/loader.test.js__

```js
/**
* @jest-environment node
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest-environment defaults to jsdom https://jestjs.io/docs/en/configuration#testenvironment-string, which is not we want here.

import compiler from './compiler.js';

test('Inserts name and outputs JavaScript', async () => {
const stats = await compiler('example.txt');
const output = stats.toJson().modules[0].source;

expect(output).toBe('export default "Hey Alice!\\n"');
expect(output).toBe('export default "Hey Alice!\"');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The example.txt should be:

Hey [name]!

Unfortunately, PrismJS would remove the line break, so I just remove the \n here.

@chenxsan chenxsan marked this pull request as ready for review May 12, 2020 14:45
@EugeneHlushko EugeneHlushko reopened this May 22, 2020
@EugeneHlushko EugeneHlushko merged commit 231746a into webpack:master May 22, 2020
@EugeneHlushko
Copy link
Member

Looking good, thanks!

@chenxsan chenxsan deleted the update-doc branch May 24, 2020 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants