最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - Starting the Server to Test an Express-Backed React App Using jest-puppeteer - Stack Overflow

programmeradmin15浏览0评论

I have an express server that serves a React app for GET requests and runs other processing for POST. I'm using light-my-request to test all of the POST functionality. The server.js file contains the following code so that the server isn't launched during tests:

const app = express();
const http = require("http").createServer(app);
if (process.env['NODE_ENV'] === 'test') {
  module.exports = {
    'app': app,
  };
} else {
  http.listen(port);
}

Tests don't need to start the server:

const result = await inject(app, {
    method: 'POST',
    url: '/'
});
expect(result).not.toBeNull();

I'd like to test that GET serves the React app by testing for some specific page content. In order to fetch the react page from my express server and check the results:

describe('A GET request to the root:', () => {
  it('should return the react app', async () => {
    // this invokes the express GET handler
    await page.goto("http://localhost:5000/");
    await expect(page).toMatchTextContent(/whatever/);
  }, 10000);
});

The jest-puppeteer docs describe how to automatically start the server, but I don't think that's what I want because I only need to run the server for the one test. Does anyone know how to do that?

UPDATE: I wonder if this stackoverflow question might be what I need. I created the jest.config.js and jest-puppeteer.config.js files for my React testing. Now I need to integrate them with my existing node tests...

I have an express server that serves a React app for GET requests and runs other processing for POST. I'm using light-my-request to test all of the POST functionality. The server.js file contains the following code so that the server isn't launched during tests:

const app = express();
const http = require("http").createServer(app);
if (process.env['NODE_ENV'] === 'test') {
  module.exports = {
    'app': app,
  };
} else {
  http.listen(port);
}

Tests don't need to start the server:

const result = await inject(app, {
    method: 'POST',
    url: '/'
});
expect(result).not.toBeNull();

I'd like to test that GET serves the React app by testing for some specific page content. In order to fetch the react page from my express server and check the results:

describe('A GET request to the root:', () => {
  it('should return the react app', async () => {
    // this invokes the express GET handler
    await page.goto("http://localhost:5000/");
    await expect(page).toMatchTextContent(/whatever/);
  }, 10000);
});

The jest-puppeteer docs describe how to automatically start the server, but I don't think that's what I want because I only need to run the server for the one test. Does anyone know how to do that?

UPDATE: I wonder if this stackoverflow question might be what I need. I created the jest.config.js and jest-puppeteer.config.js files for my React testing. Now I need to integrate them with my existing node tests...

Share Improve this question edited Feb 5 at 21:00 Rob Gravelle asked Feb 5 at 0:02 Rob GravelleRob Gravelle 2376 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Turns out that there were a few possible solutions to my problem. The easiest was to let puppeteer start the server by adding the server command to the jest-puppeteer.config.js file. I also added a command flag (--puppeteer) as described in the stackoverflow question:

server: {
  command: "node server.js --puppeteer",
  port: 5000,
  launchTimeout: 10000,
  debug: true,
},

I then added a check in the server.js file for the flag so that the server is not started for any unit tests other than those using puppeteer:

if (process.env['NODE_ENV'] === 'test'
    && !process.argv.slice(2).includes('--puppeteer')) {
  module.exports = {
    'app': app,
  };
} else {
  http.listen(port);
}

I also put my react (puppeteer) tests in a separate file just for good measure.

jest.config.js:

module.exports = {
  preset: "jest-puppeteer",
  testMatch: [
    "**/__tests__/test.js",
    "**/__tests__/testReactClient.js"
  ],
  verbose: true
}

Feel free to comment if you have a better solution.

发布评论

评论列表(0)

  1. 暂无评论