-
Notifications
You must be signed in to change notification settings - Fork 3
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
Отключить injectGlobals в Jest #358
Comments
что-то не так UPD: помогли |
еще есть вот такая сложность |
krutoo
added a commit
that referenced
this issue
Oct 19, 2023
krutoo
added a commit
that referenced
this issue
Oct 19, 2023
krutoo
added a commit
that referenced
this issue
Oct 19, 2023
krutoo
added a commit
that referenced
this issue
Oct 20, 2023
krutoo
added a commit
that referenced
this issue
Oct 20, 2023
скрипт для правки тестов: import fs from 'node:fs/promises';
import glob from 'fast-glob';
/**
* Добавляет контент в начало файла.
* @param {string} filename Путь до файла.
* @param {string} content Контент для добавления в начало.
*/
async function prependFile(filename, content) {
const data = await fs.readFile(filename);
const handle = await fs.open(filename, 'w+');
const insert = Buffer.from(content);
await handle.write(insert, 0, insert.length, 0);
await handle.write(data, 0, data.length, insert.length);
await handle.close();
}
glob('src/**/*.test.{ts,tsx}').then(list =>
Promise.all(
list.map(filename =>
fs.readFile(filename, 'utf-8').then(source => {
const imports = ['it', 'expect', 'describe'];
source.includes('beforeAll') && imports.push('beforeAll');
source.includes('afterAll') && imports.push('afterAll');
source.includes('beforeEach') && imports.push('beforeEach');
source.includes('afterEach') && imports.push('afterEach');
source.includes('jest') && imports.push('jest');
return prependFile(filename, `import { ${imports.join(', ')} } from '@jest/globals';\n`);
}),
),
),
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ситуация
Сейчас в тесты инжектятся глобальные переменные Jest
у этого есть ряд минусов
например они также подсказываются в НЕ тестах
лучше импортировать явно
Необходимо
задать опцию
injectGlobals: false
в конфиге Jestпройтись по тестам и с помощью примерно такого скрипта докинуть импорты:
The text was updated successfully, but these errors were encountered: