Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2243 clarify transpile.md #2244

Merged

Conversation

michael-lloyd-morris
Copy link
Contributor

🤔 What's changed?

Clarified the differences between transpiling Cucumber in CommonJS and ESM

⚡️ What's your motivation?

Spent 4 hours slogging through this.

🏷️ What kind of change is this?

  • 🏦 Refactoring/debt/DX (improvement to code design, tooling, documentation etc. without changing behaviour)

♻️ Anything particular you want feedback on?

Is this rewrite clear to others?

📋 Checklist:


This text was originally generated from a template, then edited by hand. You can modify the template here.

@coveralls
Copy link

coveralls commented Feb 15, 2023

Coverage Status

Coverage: 98.489%. Remained the same when pulling 53f2410 on michael-lloyd-morris:2243-clarify-transpile.md into 764b7b6 on cucumber:main.

Cucumber needs typescript to be transpiled to the CommonJS format for the examples as given in this document to work.
More elaborate retooling for clarity.
@Jeff-Tian
Copy link
Member

Thanks, @michael-lloyd-morris! Great work, it'll benefit many others unfamiliar with nodejs and esm etc.

Copy link
Member

@Jeff-Tian Jeff-Tian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Very helpful messages indeed.


```javascript
export default {
requireModule: ['ts-node/register'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn’t seem right? require-module doesn’t affect import

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can just be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be reworded heavily and I've done so. If you read that header as applying to all ESM projects others will too. That sub header is level 4 and it was meant to cover "with TS-Node to load ES modules"


```shell
$ NODE_OPTIONS="--loader ts-node/esm" cucumber-js --import 'step-definitions/**/*.ts'
Your cucumber.js file will need to be an ESM module and contain the following at a minimum:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config file can still be JSON or YAML - I guess we want to say that if JavaScript then it should be the appropriate format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines removed.


```json
{
"scripts: {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a double quote on the key here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Sorry. Also spotted an uncapitalized T at the start of a sentence so I sent the whole thing through a spell/grammar checker to be sure. Should be good now.

@michael-lloyd-morris
Copy link
Contributor Author

michael-lloyd-morris commented Mar 29, 2023

These examples are fairly complex and they also raise a lot of Google and Bing search results. Perhaps linking to the relevant cucumber-js-examples would be in order? If so I need to add an example for the somewhat gnarly ts-node with esm modules scenario.

@michael-lloyd-morris
Copy link
Contributor Author

@davidjgoss Please let me know what else do I need to do for this.


You'll also need to specify where your support code is, since `.ts` files won't be picked up by default.
Cucumber doesn't load `*.ts` files by default. The glob pattern `step-definitions/**/*.ts` will load them from the default step definition location.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this line is an improvement. step-definitions isn't any of our default paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted. I wasn't aware of that btw - many of the examples follow this pattern.


- In a configuration file `{ requireModule: ['ts-node/register'], require: ['step-definitions/**/*.ts'] }`
- On the CLI `$ cucumber-js --require-module ts-node/register --require 'step-definitions/**/*.ts'`

#### ESM
If you are using ts-node in a CommonJS project then this configuration will work, but if you have an ESM project there are additional steps.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should make clear that both requireModule and require are not suitable for ESM and that the --loader flag and import option are their respective equivalents.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

```

Don't forget to set your `tsconfig.json` to emit JavaScript with `import` and `export` statements:
That last step requires setting an environment variable. The cleanest way to do this is to include the [cross-env](https://www.npmjs.com/package/cross-env) package with `npm i -D cross-env`. With that package installed make the following change to your npm test script invocation in the package.json file:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should be prescriptive of using this particular library/approach. By all means mention it as an option but examples should be as vanilla as possible really.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@michael-lloyd-morris
Copy link
Contributor Author

Sorry about the lag. Requested updates now pushed.

In particular, we don't have a `loader` option - this can't currently be set in-process so in practise we need to reply on users setting the environment variable (or, invoking `node` directly, but that's not an example I would document).
@davidjgoss davidjgoss merged commit c65dfe0 into cucumber:main Apr 21, 2023
9 of 10 checks passed
@michael-lloyd-morris michael-lloyd-morris deleted the 2243-clarify-transpile.md branch April 21, 2023 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants