项目作者: michaelthe

项目描述 :
puppeteer + jest + awesome-code = puppyjs
高级语言: JavaScript
项目地址: git://github.com/michaelthe/puppyjs.git
创建时间: 2018-06-05T12:41:06Z
项目社区:https://github.com/michaelthe/puppyjs

开源协议:MIT License

下载


PuppyJS | Docs :notebook:

js-standard-style
npm
npm
GitHub last commit (branch)
CircleCI branch

Puppeteer + Jest + awesome-code = Puppy.JS

PuppyJS is a framework agnostic E2E (end-to-end) testing and mocking tool for front end developers.
Puppy depends on Jest for tests and Puppeteer
for the testing environment so if you know these tools then you already know 80% of Puppy.

Puppy also lets you mock HTTP APIs and web socket events so you can
develop your application until the backend is ready as well as
run your E2E tests against the same mock API and socket events you used for development.

Getting Started

Install

  1. npm install puppyjs --save-dev

Install globally

  1. npm install puppyjs --global

Get some help

  1. puppy --help

Run mocking servers

  1. puppy serve

Run tests

  1. puppy test

Sample directory structure

Below you can find a sample directory structure. The important thing to notice are the puppy.api.js, puppy.ws.js and puppy.config.js and that they are at the root level of the directory.

  1. .
  2. |
  3. ├── puppy.config.js <optional>
  4. ├── puppy.api.js <optional>
  5. ├── puppy.ws.js <optional>
  6. |
  7. ├── package.json
  8. |
  9. ├── dist
  10. | ├── background.jpg
  11. | ├── index.html
  12. | └── fonts
  13. |
  14. └── tests
  15. ├── users.pup.js
  16. └── notifications.pup.js

puppy.api.js

Sample:

  1. module.exports = {
  2. '/api/users': {
  3. 'GET': {
  4. headers: {
  5. 'Authorization': 'Bearer some-token'
  6. },
  7. status: 200,
  8. body: 'hello its a GET'
  9. }
  10. }
  11. }

puppy.ws.js

Sample:

  1. module.exports = {
  2. 'notification': {
  3. delay: 1000,
  4. interval: 1000,
  5. message: [
  6. {seen: false, createdAt: Date.now(), text: 'I am a notification'}
  7. ]
  8. }
  9. }

puppy.config.js

Sample:

  1. module.exports = {
  2. port: 1337
  3. }

Your first End-to-End test

Underneath, Puppy uses Jest for asserting and Puppeteer for executing actions in the browser. Please head to their documentation if you are not familiar.
In the example below it assumes a file index.html inside src folder and a file with any name but ends with .pup.js which will hold the test.

  1. describe('test', () => {
  2. let page
  3. it('check that puppy works', async () => {
  4. page = await puppy.newPage('http://localhost:1337/src/index.html') // page instance is a puppeteer page instance
  5. ... your code
  6. expect(...) // Jest
  7. })
  8. })

To run this use the command

  1. puppy test

Puppy Development Mock Server

You can use the same puppy.api.js file that you configure above for development purpose. Run puppy serve and you can now make a GET request to /api/users and get a reply back as set in the puppy.api.js file.