- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.1k
Description
👓 What did you see?
I have setup a Angular 13 project that uses both cucumber-js and playwright. I use cucumber-js as the test runner. This project uses ES Modules as I have "type":"module" in my package.json.
When I try to run my tests with cucumber-js I get ES Module errors
✅ What did you expect to see?
Should be able to run my cucumber tests
📦 Which tool/library version are you using?
Tried also cucumber version 7.3.0 and 8.0.0-rc3. below my package.json
{
"name": "cucumber-playwright",
"version": "1.0.0",
"type": "module",
"scripts": {
"video": "PWVIDEO=1 cucumber-js",
"format": "prettier --write \"/*.{ts,tsx,css,html}\" ",
"only": "npm run cucumber -- --tags @only",
"steps-usage": "cucumber-js features//.feature --dry-run",
"all": "cucumber-js features/**/.feature",
"test-chrome": "cucumber-js",
"test-firefox": "SET BROWSER=firefox && cucumber-js",
"test:parallel": "cucumber-js --parallel=2",
"test-generate-report": "node src/support/reportHTML.js && node src/support/reportJUNIT.js"
},
"dependencies": {
"@cucumber/cucumber": "8.0.0-rc.3",
"@cucumber/html-formatter": "18.0.0",
"@cucumber/pretty-formatter": "1.0.0-alpha.2",
"@types/fs-extra": "9.0.13",
"cucumber-console-formatter": "1.0.0",
"cucumber-html-reporter": "5.5.0",
"expect": "27.5.1",
"playwright": "1.20.1"
},
"devDependencies": {
"@types/chai": "^4.3.0",
"@types/expect": "24.3.0",
"@types/lodash": "4.14.180",
"@types/node": "16.11.26",
"@typescript-eslint/eslint-plugin": "5.16.0",
"@typescript-eslint/parser": "5.16.0",
"allure-commandline": "^2.17.2",
"chai": "^4.3.6",
"cucumber-html-reporter": "^5.5.0",
"cucumber-junit-convert": "^2.1.1",
"cucumber-junit-formatter": "^0.2.2",
"cucumberjs-junitxml": "^1.0.0",
"e2e-api-playwright": "file://D:/cucumber-playwright-master/e2e-api-playwright-1.0.0.tgz",
"eslint": "8.12.0",
"eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.25.4",
"eslint-plugin-prettier": "4.0.0",
"fs-extra": "10.0.1",
"open": "8.4.0",
"prettier": "2.6.1",
"rimraf": "3.0.2",
"standard-version": "9.3.2",
"ts-node": "^10.7.0",
"typescript": "4.6.3"
}
🔬 How could we reproduce it?
This is the way my project looks like
features
->playwright.feature
node_modules
reports
screenshots
src
->steps
---->playwright.steps.ts
->support
---->common-hooks.ts
---->config-chrome.ts
---->config-firefox.ts
---->config.ts
---->custom-world.ts
cucumber.js
tsconfig.json
package.json
e2e-api-playwright-1.0.0.tgz
tsconfig.json:
{
"compilerOptions": {
"baseUrl": "./",
"target": "es2020",
"module": "commonjs",
"sourceMap": true,
"outDir": "./build",
"noEmit": true,
"strict": true,
"noImplicitAny": false,
"noImplicitThis": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"skipDefaultLibCheck": true,
"resolveJsonModule": true,
"lib": [
"dom",
"es2020"
],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true
},
"exclude": [
"node_modules"
]
}cucumber.js:
const common = `
  --require-module ts-node/register
  --require-module tsconfig-paths/register
  --require src/**/*.ts
  --require support/config.ts
  --format json:reports/report.json 
  --format html:reports/cucumber-html-report.html
  --format summary 
  --format progress-bar 
  --format @cucumber/pretty-formatter
  --format-options ${JSON.stringify({ snippetInterface: 'async-await' })}
  --publish-quiet
  `;
const getWorldParams = () => {
  const params = {
    foo: 'bar',
  };
  return `--world-parameters ${JSON.stringify({ params })}`;
};
module.exports = {
  default: `${common} ${getWorldParams()}`,
};playwright.feature:
@foo
Feature: Playwright tests
Background:
Given Go to the playwright website
@runME
Scenario: Failing test
Then I get an error
Scenario: Passing test
Then I get a passing testplaywright.steps.ts:
import { ICustomWorld } from '../support/custom-world';
import { config } from '../support/config';
import { Given, Then } from '@cucumber/cucumber';
import expect from 'expect';
import { Placeholder } from 'e2e-api-playwright';
let myPO: Placeholder;
myPO = new Placeholder('Emma');
Given('Go to the playwright website', async function (this: ICustomWorld) {
const page = this.page!;
await page.goto(config.BASE_URL);
await page.locator('nav >> a >> text="Playwright"').waitFor();
await myPO.setText();
});
Then('I get an error', async function () {
expect(true).toEqual(false);
})
Then('I get a passing test', async function () {
expect(true).toEqual(true);
})Steps to reproduce the behavior:
- Do an npm install (this installs also the page object library - e2e-api-playwright)
- Try to run the tests via npm run test-chrome
- See error 'Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\cucumber-playwright-master\src\steps\playwright.steps.ts
 require() of ES modules is not supported.
 require() of D:\cucumber-playwright-master\src\steps\playwright.steps.ts from D:\cucumber-playwright-master\node_modules@cucumber\cucumber\lib\api\support.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
 Instead change the requiring code to use import(), or remove "type": "module" from D:\cucumber-playwright-master\package.json.'
- If I remove from package.json the "type": "module" then I get error importing the PO. The error is: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: D:\cucumber-playwright-master\node_modules\e2e-api-playwright\fesm2015\e2e-api-playwright.mjs
📚 Any additional context?
Details related to the e2e-api-playwright library:
e2e-api-playwright
->src
---->lib
-------->placeholder.ts
---->public_api.ts
ng-package.json
package.json
tsconfig.lib.json
placeholder.ts:
export class Placeholder {
public t: string;
constructor(private el: string) {
this.t = this.el;
}
public setText() {
console.log('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' + this.t);
}
}
public_api.ts:
export { Placeholder } from './lib/placeholder';
tsconfig.lib.json:
{
"compilerOptions": {
"outDir": "../out-tsc/lib",
"baseUrl": ".",
"declarationMap": true,
"target": "es2020",
"module": "es2020",
"moduleResolution": "node",
"declaration": true,
"sourceMap": true,
"inlineSources": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"importHelpers": true,
"types": [],
"lib": [
"dom",
"es2020"
]
},
"angularCompilerOptions": {
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true
},
"exclude": [
"src/test.ts",
"**/*.spec.ts"
]
}