Skip to content

Commit

Permalink
docs: clarify how to use aw.mock (#506)
Browse files Browse the repository at this point in the history
Ensure using `aw.mock` correct to calculate correct coverage results
  • Loading branch information
stoffeastrom committed Mar 6, 2020
1 parent 17a622e commit 39e161f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,53 @@ const [{ default: FancyButton }] = aw.mock(

Look at the React [example](https://github.com/qlik-oss/after-work.js/tree/master/examples/react) and especially [**here**](https://github.com/qlik-oss/after-work.js/blob/master/examples/react/test/fancy-button.spec.js) for more details.

> If you are using `coverage` ensure you are only calling `aw.mock` in module scope or in a `before` function. If not, it will be instrumented multiple times and the last instrumentation wins, which will lead to incorrect coverage results. For example:
```javascript
import foo from 'foo';

const span = <span>my span</span>;

// Module scope 👍
const [{ default: FancyButton }] = aw.mock(
[
// Mock components
[require.resolve('../src/fancy-button.js'), () => () => span]
],
// Require components
['../src/fancy-button']
);

describe(() => {
it('should calculate correct coverage', () => {});
});
```

or

```javascript
import foo from 'foo';

const span = <span>my span</span>;

describe(() => {
let FancyButton;
before(() => {
// In before 👍
const [{ default: FancyButton }] = aw.mock(
[
// Mock components
[require.resolve('../src/fancy-button.js'), () => () => span]
],
// Require components
['../src/fancy-button']
);

});
it('should calculate correct coverage', () => {});
});
```

## Snapshot Testing

We are using the awesome 📸 [**jest-snapshot**](https://github.com/facebook/jest/tree/master/packages/jest-snapshot) package.
Expand Down
47 changes: 47 additions & 0 deletions website/versioned_docs/version-6.0.0/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,53 @@ const [{ default: FancyButton }] = aw.mock(

Look at the React [example](https://github.com/qlik-oss/after-work.js/tree/master/examples/react) and especially [**here**](https://github.com/qlik-oss/after-work.js/blob/master/examples/react/test/fancy-button.spec.js) for more details.

> If you are using `coverage` ensure you are only calling `aw.mock` in module scope or in a `before` function. If not, it will be instrumented multiple times and the last instrumentation wins, which will lead to incorrect coverage results. For example:
```javascript
import foo from 'foo';

const span = <span>my span</span>;

// Module scope 👍
const [{ default: FancyButton }] = aw.mock(
[
// Mock components
[require.resolve('../src/fancy-button.js'), () => () => span]
],
// Require components
['../src/fancy-button']
);

describe(() => {
it('should calculate correct coverage', () => {});
});
```

or

```javascript
import foo from 'foo';

const span = <span>my span</span>;

describe(() => {
let FancyButton;
before(() => {
// In before 👍
const [{ default: FancyButton }] = aw.mock(
[
// Mock components
[require.resolve('../src/fancy-button.js'), () => () => span]
],
// Require components
['../src/fancy-button']
);

});
it('should calculate correct coverage', () => {});
});
```

## Snapshot Testing

We are using the awesome 📸 [**jest-snapshot**](https://github.com/facebook/jest/tree/master/packages/jest-snapshot) package.
Expand Down

0 comments on commit 39e161f

Please sign in to comment.