jest icon indicating copy to clipboard operation
jest copied to clipboard

Question about transformers and import context

Open macku opened this issue 8 years ago • 5 comments

I'm trying to write a plugin for Jest. Currently, I'm trying to understand if there is a way for a custom transformer to know what was the context that it was imported/required.

Let say we have this dummy pseudo module and test:

moo.js

import foo from './foo';
import './bar.extension';

exports default function() {
	return foo.doSomething();
}

and the test moo.test.js:

import moo from './moo';

test('Test Moo', () => {
	expect(moo).toBe(true);
});

and custom jest.config.js:

{
	"transform": {
		"\\.extension$": "custom-extension-transformer"
	}
}

and custom-extension-transformer.js:

module.exports = {
	process(src, filename) {
		// Who did imported/required me?

		return '';
	}
};

Is there a way that a custom-extension-transformer.js module will know that is was imported from the moo.js or moo.test.js file? Maybe there is some other way to use Jest internals in order to discover the require context?

macku avatar Feb 20 '18 05:02 macku

We know it internally, but it's not passed along to the code invoking transformers here: https://github.com/facebook/jest/blob/9df3035829299a93579bad8cfb1db2081617ea6e/packages/jest-runtime/src/index.js#L525

What's your use case?

SimenB avatar Feb 20 '18 05:02 SimenB

I'm working on the plugin for React that is using Puppeteer for running integration tests called jest-puppy-shots. I would like to use a transformer feature to get the CSS/Less/Sass styles from the imported module. It might be just a simple import like ex. import './foo.css'. Something like a webpack loader. Then, use a transformer to generate and intercept those results (CSS) and send the content to Puppeteer. For that purpose, I would need to know where the CSS/Less/Sass was imported so I can put in the correct page context. I'm thinking about creating a required change in Jest and providing a Pull Request. The idea is that a transformer function can be smarter about the context from where it was imported.

macku avatar Feb 20 '18 10:02 macku

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] avatar Feb 25 '23 11:02 github-actions[bot]

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

github-actions[bot] avatar Mar 27 '23 11:03 github-actions[bot]

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

github-actions[bot] avatar Apr 27 '23 00:04 github-actions[bot]