我正在开发类似于live-reload / browser-sync的nodejs库,并且正在使用 jest-puppeteer 进行自动化测试。
当我手动测试库,打开浏览器并修改文件voilá时,页面会刷新(通过插入的代码运行,该代码在通过location.reload( true )
接收到信号时会运行// "reloader" is my library
import reloader from './../src/index';
import * as fs from 'fs';
import { promisify } from 'util';
const read = promisify( fs.readFile )
const write = promisify( fs.writeFile )
test('1. Refresh when file changes', async () => {
const server = await reloader( { dir: 'test/01' } );
await page.goto( 'http://localhost:' + server.port );
// This test passes
await expect( page.title()).resolves.toMatch( 'Old title' );
// Read and modify index.html to generate a refresh
const file = 'test/01/index.html'
const content = await read( file, 'utf8' );
await write( file, content.replace( 'Old title', 'New title' ) );
// Wait the page to refresh
await page.waitForNavigation( { waitUntil: 'networkidle2' } )
// This test doesn't pass
// Still receiving "Old title"
await expect( page.title()).resolves.toMatch( 'New title' );
// Undo the changes
write( file, content );
});
websocket)。
但是当我用Jest运行测试时,似乎Puppeteer无法获得刷新。
New title
在上一次测试中,我仍然收到index.html
Old title
(已正确写入<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="CommuteEdit.aspx.cs" Inherits="Web.CommuteEdit" %>
文件中)。
答案 0 :(得分:0)
测试失败,因为最后一部分没有运行注释\\ Undo the changes
,并且测试文件保留在New title
中。
通过以下测试,它可以完美运行:
import reloader from './../src/index';
import * as fs from 'fs';
import { promisify } from 'util';
const read = promisify( fs.readFile )
const write = promisify( fs.writeFile )
test('1. Refresh when file change', async () => {
// If this test failed previously, lets guarantee that
// everything is correct again before we begin
const file = 'test/01/index.html'
const content = ( await read( file, 'utf8' ) ).replace( 'New title', 'Old title' );
await write( file, content );
const server = await reloader( { dir: 'test/01' } );
await page.goto( 'http://localhost:' + server.port );
await expect( page.title()).resolves.toMatch( 'Old title' );
// Read and modify index.html to generate a refresh
await write( file, content.replace( 'Old title', 'New title' ) );
// Wait the page to refresh
await page.waitForNavigation( { waitUntil: 'networkidle2' } )
await expect( page.title()).resolves.toMatch( 'New title' );
// Undo the changes
write( file, content );
});