xhr-snapshot
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

xhr-snapshot

Create snapshot of XMLHttpRequest for testing.

Usage

  1. Setup jest
# jest.config.js

module.exports = {
  testEnvironment: 'jsdom',
  testEnvironmentOptions: {
    url: '%YOUR SISE ORIGIN%'
  },
  setupFilesAfterEnv: ['<rootDir>/jest.setup.js']
}
# jest.setup.js

import 'whatwg-fetch';
import { resolve } from 'path';
import { configure } from 'xhr-snapshot';

configure({
  snapshotDir: resolve(__dirname, '__xhr_snapshots__'),
  updateSnapshot: process.argv.includes('--update-snapshot'),
})
  1. Run your jest tests with actual fetch requests.
import { normalize } from '../src/normalize';

describe('awesome tests', () => {

  it('should create the normalized state', () => {
      const response = await fetch('...').then(res => res.json());
      expect(normalize(response)).toMatchSnapshot();
  });

});

How does it works?

In the current jsdom environment, all ajax requests is done via XMLHttpRequest.

So we can hijack it via replacing window.XMLHttpRequest.

The XMLHttpRequest hijacking is done via nise module provided by the SinonJS team.

The fetch isn't defined in current Node.js and jsdom environment so your fetch request will be passed for XMLHttpRequest via whatwg-fetch.

Readme

Keywords

none

Package Sidebar

Install

npm i xhr-snapshot

Weekly Downloads

0

Version

0.0.3

License

MIT

Unpacked Size

23 kB

Total Files

12

Last publish

Collaborators

  • hrsh7th