【问题标题】:Error: Cannot find module '@playwright/test'错误:找不到模块“@playwright/test”
【发布时间】:2022-01-09 09:04:27
【问题描述】:

我目前正在尝试通过使用 const { expect } = require('@playwright/test'); 来使用 expect 来进行断言,但每次我得到错误:找不到模块“@playwright/test”。这是一个非常短的脚本,但其中有问题。

const { chromium } = require("playwright");
const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

(async () => {
  const browser = await chromium.launch({
    headless: false,
  });
  const page = await browser.newPage();
  await page.goto("someurl");
  await page.fill("input[name='userLoginId']", 'nnn');
  await page.fill("input[name='password']", 'nnn');
  await page.click("button[type=submit]");
})();

package.json

{
  "name": "playwright",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "node ./index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "playwright": "^1.15.1",
    "playwright-expect": "^0.1.2"
  }
}

没有这个测试可以正常工作:

const { expect } = require('@playwright/test');
const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

它会做我要求它做的事情,但现在我想做断言并添加它,现在它不起作用。

【问题讨论】:

  • 如果有帮助,您可能已经知道,您正在使用命名导入,因此您指定的名称,即expect,必须与从模块@playwright/test。所以1)文件真的命名为@playwright/test吗? 2)它是否导出值expect
  • 嗨,我进入节点模块,playwright -> test -> expect.js 发现这个 const expect = _expect.default;出口. 期望 = 期望;所以我认为他们是正确的?我也忘了提到我使用的是 M1 Pro,曾经有一段时间我在导入时也遇到了问题,但如果 M1 Pro 有任何事情要做,我想知道
  • 是的,对不起,我不熟悉这个包,它看起来应该可以工作,如果你按照这里的所有说明操作:playwright.dev/docs/intro
  • 不需要道歉,我这样做有点不同,而不是使用 npx 命令只是 npm run test 并且只是在 main.js 上做所有事情,所以这就是为什么我很好奇为什么会出现这个错误正在发生。

标签: javascript node.js playwright playwright-test


【解决方案1】:
  1. 你必须安装@playwright/test库:
   npm i -D @playwright/test
  1. 不要使用playwright-expect 库。 Playwright 已经包含web-first assertions。因此,没有理由使用额外的库来扩展 expect。

  2. 删除未使用的代码:

const { matchers } = require('playwright-expect');
console.log("##########", expect)

// add custom matchers
expect.extend(matchers);

【讨论】:

    猜你喜欢
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2018-12-30
    • 1970-01-01
    • 2016-12-20
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多