
npm install packateerIt provides a function that requires a file (usually a JSX or TSX file) as input for a WebpackDevServer instance to host after transpiling and compilation. Puppeteer is used to manage the browser which is directed to the url of this Webpack server. The Puppeteer and WebpackDevServer instances created in this process are returned along with the port used by this server.
One use case would be the need or preference to test against a browser as an alternative to other options.
An example is provided in the example_usage folder and listed below
You can take one or more components ( App.js contains one for this example )
Then organize these components in the entry point file. This is passed as an argument to the WebpackDevServer.=.
In the example the tab ordering is tested.
!Browser tabbing through DIV elements
``
import { CreateServerAndClient } from 'packateer';
import * as path from 'path';
import * as util from 'util';
jest.setTimeout(50000);
function findFocusedNodes(node) {
if (node.focused) {
return [node]
} else if (node.children && node.children.length) {
// Array.prototype.flatMap = function(mapFunction) { return this.map(mapFunction).reduce((a, c) => { return [ ...a, ...(Array.isArray(c) ? c : [c]) ]; }, []) }
return node.children.map(n => findFocusedNodes(n)).reduce((a, c) => { return [ ...a, ...(Array.isArray(c) ? c : [c]) ]; }, []);
} else {
return [];
}
}
async function delay(delayTime) {
await new Promise((res, rej) => {
setTimeout(() => {
res(true)
}, delayTime)
});
}
let [ppage, pserver, pbrowser] = [undefined, undefined, undefined];
describe('test with pup', () => {
beforeAll(async () => {
const { port, server, browser, page } = await CreateServerAndClient(undefined, false, path.join(__dirname, 'App.subpage.jsx'), [path.join(__dirname, '../node_modules')]);
pserver = server;
ppage = page;
pbrowser = browser;
})
afterAll(() => {
pbrowser && pbrowser.close();
pserver && pserver.close();
})
test('renders learn react link', async () => {
await ppage.waitForSelector('#initial');
await ppage.focus("#initial");
await delay(500);
let snapNode = await ppage.accessibility.snapshot();
let inFocus = findFocusedNodes(snapNode);
expect(inFocus.length).toEqual(1);
expect(inFocus[0].name).toEqual("initial");
ppage.keyboard.press('Tab');
await delay(500);
snapNode = await ppage.accessibility.snapshot();
inFocus = findFocusedNodes(snapNode);
expect(inFocus.length).toEqual(1);
expect(inFocus[0].name).toEqual("First");
ppage.keyboard.press('Tab');
await delay(500);
snapNode = await ppage.accessibility.snapshot();
inFocus = findFocusedNodes(snapNode);
expect(inFocus.length).toEqual(1);
expect(inFocus[0].name).toEqual("Second");
});
});
`
`
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
ReactDOM.render(
,
document.getElementById('root')
);
`
`
// import React from 'react'
import logo from './logo.svg';
import './App.css';
function App() {
return (
<>
Hey, this is a test.
export default App;
``