question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

globalSetup.ts script ignores tsconfig.json

See original GitHub issue

Issue

When tsconfig.json has defined compilerOptions.baseUrl, absolute paths can be used within test files for importing items. However, same TS code placed in globalSetup script handles only relative paths. Looks like tsconfig is simply ignored while running globalSetup.

To Reproduce

Steps to reproduce the behavior:

  1. checkout https://github.com/4ekki/jest-globalSetup-import-bug. Note that first line of scripts/globalSetup.ts file is : import { sleep } from "../tests/src"
  2. run yarn test smoke
  3. change first line of scripts/globalSetup.ts file to: import { sleep } from "src"
  4. run yarn test smoke

Actual behavior

On 2, globalSetup script is run successfully with relative path. Also, import with absolute path is successfully handled in test file. image image

On 4, globalSetup script fails to import sleep from src, using absolute path: image

Expected behavior

On 4, absolute paths in globalSetup.ts are resolved using compilerOptions.baseUrl property from tsconfig.json

Minimal repo

https://github.com/4ekki/jest-globalSetup-import-bug

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:15
  • Comments:18

github_iconTop GitHub Comments

19reactions
Eoksnicommented, Nov 29, 2019

Adding require("tsconfig-paths/register"); at the top of globalSetup seems to fix it. This works for me globalSetup.ts:

require("tsconfig-paths/register");

import logger from "src/logger";
...

export = async function(): Promise<void> {
  logger.log('Jest Global Setup');
};
5reactions
ahnpnlcommented, Sep 6, 2019

Hmm look like moduleNameMapper only works in test files and doesn’t seem to work with globalSetup.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is the TypeScript compiler ignoring tsconfig.json?
I am really to new to Gulp, but the Gulp task looks OK to me: var tsProject = ts.createProject('scripts/tsconfig.json'); gulp.task( ...
Read more >
How to configure Jest with TypeScript | Swizec Teller
Tell TypeScript to ignore Jest files. Jest encourages you to put test files next to the code you're testing. Makes tests easier to...
Read more >
Configuring Jest
To read TypeScript configuration files Jest requires ts-node . Make sure it is installed in your project. The configuration also can be ...
Read more >
Setup - Testing Library
json . If you're using Create React App without TypeScript, save this to jsconfig.json instead. tsconfig.json.
Read more >
Advanced: configuration | Playwright - CukeTest
Specify globalSetup , baseURL and storageState in the configuration file. TypeScript; JavaScript. // playwright.config.tsimport ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found