Skip to content

Commit

Permalink
Merge pull request #63 from sparksuite/final-tweaks
Browse files Browse the repository at this point in the history
Final tweaks
  • Loading branch information
WesCossick authored Mar 25, 2021
2 parents 48797f5 + 131b846 commit 97d5fce
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/utils/get-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getConfig from './get-config';
const testFileTreesPath = path.normalize(path.join(__dirname, '..', '..', 'test-file-trees'));

// Tests
describe('#getConfig(true)', () => {
describe('#getConfig()', () => {
it('Handles no config file', async () => {
jest.spyOn(process, 'cwd').mockReturnValue(path.join(testFileTreesPath, 'no-config'));

Expand Down
2 changes: 1 addition & 1 deletion src/utils/get-context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import getContext from './get-context';
const testFileTreesPath = path.normalize(path.join(__dirname, '..', '..', 'test-file-trees'));

// Tests
describe('#getContext(true)', () => {
describe('#getContext()', () => {
it('Skips reconstructing the context when possible', async () => {
jest.spyOn(process, 'cwd').mockReturnValue(path.join(testFileTreesPath, 'primary'));

Expand Down
2 changes: 2 additions & 0 deletions website/docs/suggested-projects/cjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ slug: /suggested-projects/cjs

- Your package runs in Node.js
- Your package is expected to be imported/required by projects
- Your package uses a conditional export for the Common JS module system
- Via the `exports` key in the `package.json` file

## What to test

Expand Down
45 changes: 44 additions & 1 deletion website/docs/suggested-projects/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ slug: /suggested-projects/esm

- Your package runs in Node.js
- Your package is expected to be imported/required by projects
- Your package uses a conditional export for the ES module system
- Via the `exports` key in the `package.json` file

## What to test

Expand All @@ -27,7 +29,7 @@ Specify the module type in the `package.json` file:

## Common problems to watch for

### Import syntax
### External import syntax

Make sure that your package can be imported successfully and without using `* as`.

Expand All @@ -36,6 +38,47 @@ import yourPackage from 'your-package'; // Good
import * as yourPackage from 'your-package'; // Bad
```

### Internal import syntax

This is especially important if you’re using a compiler/transpiler like TypeScript. Make sure that internal imports don’t throw `ERR_MODULE_NOT_FOUND` errors.

Consider the following two TypeScript files:

```ts title="./bar.ts"
export default function bar() {
return 'bar';
}
```

```ts title="./foo.ts"
import bar from './bar';
bar();
```

Consider also the following configuration directives:

```json {3-4} title="tsconfig.json"
{
"compilerOptions": {
"module": "ESNext",
"target": "ES2020"
}
}
```

```json {2} title="package.json"
{
"type": "module",
}
```

If you run tests directly against the TypeScript source files with a test runner like Jest, chances are your tests will pass. But, when you run the compiled version, you may encounter `ERR_MODULE_NOT_FOUND` errors stating that the `./bar` file can't be found. To fix this (in this scenario), you must add `.js` to the end of every import path:

```ts {1} title="./foo.ts"
import bar from './bar.js';
bar();
```

### Use of `require()`

If your package attempts to `require()` a file from the project using your package, you may encounter a `ERR_REQUIRE_ESM` error. This is because you can’t use `require()` to load an ES module.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/suggested-projects/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ slug: /suggested-projects/typescript

## When to test

- Your package could be run in a project written using TypeScript. Since TypeScript is a superset of JavaScript, most, if not all, JavaScript packages will work in TypeScript projects, so this environment is highly recommended.
- Your package could be run in a project written using TypeScript
- Since TypeScript is a superset of JavaScript, most, if not all, JavaScript packages will work in TypeScript projects

## What to test

Expand Down

0 comments on commit 97d5fce

Please sign in to comment.