jest

How to fix TypeError is not a function (testing promises with Jest)

隐身守侯 提交于 2019-12-10 13:27:30
问题 I have a passing test now thanks to the answer here: How to test is chained promises in a jest test? However I'm still getting an error in the catch part of my test. I seem to not be able to correctly mock or spy this part in the actions file: .then(res => res.getIdToken()) TEST signIn ERROR => TypeError: res.getIdToken is not a function The Test jest.mock('services/firebase', () => new Promise(resolve => resolve({ signInWithEmailAndPassword: () => Promise.resolve({ getIdToken: 'abc123' }),

React Native - mocking FormData in unit tests

百般思念 提交于 2019-12-10 03:39:52
问题 I'm having issues testing my thunks, as many of my API calls are using FormData, and I can't seem to figure out how to mock this in tests. I'm using Jest. My setup file looks like this: import 'isomorphic-fetch'; // Mocking the global.fetch included in React Native global.fetch = jest.fn(); // Helper to mock a success response (only once) fetch.mockResponseSuccess = body => { fetch.mockImplementationOnce(() => Promise.resolve({ json: () => Promise.resolve(JSON.parse(body)) }) ); }; // Helper

Why Jest's toThrow won't work when create an instance of a ES6 class directly in the constructor?

二次信任 提交于 2019-12-09 15:55:50
问题 class TestObject { constructor(value) { if (value === null || value === undefined) { throw new Error('Expect a value!'); } } } describe('test the constructor', () => { test('it works', () => { expect(() => { new TestObject(); }).toThrow(); }); test('not work', () => { expect(new TestObject()).toThrow(); }); }); 2 Test cases here, one works and the other not. The failing message for the not work one is as the following: ● test the constructor › not work Expect a value! at new TestObject (tests

Invalidate node cache when using Jest

穿精又带淫゛_ 提交于 2019-12-08 15:45:09
问题 I have a file with object that gets populated with process.env properties: env.js console.log('LOADING env.js'); const { PROXY_PREFIX = '/api/', USE_PROXY = 'true', APP_PORT = '8080', API_URL = 'https://api.address.com/', NODE_ENV = 'production', } = process.env; const ENV = { PROXY_PREFIX, USE_PROXY, APP_PORT, API_URL, NODE_ENV, }; module.exports.ENV = ENV; Now I try to test this file with different process.env properties: env.test.js const envFilePath = '../../config/env'; describe(

Jest test (ReferenceError: google is not defined) ReactJS and Google Charts

风流意气都作罢 提交于 2019-12-08 12:24:51
问题 I'm using the google script tag from their CDN (tried body and head) <script src="https://wikitags.com/js/googlecharts.min.js"></script> The Google Chart in my app works fine, however it's causing my Jest tests to fail... Inside of the <ChartComponent /> componentDidMount() { // Load the Visualization API and the corechart package. console.log('Chart mounted'); google.charts.load('current', { packages: ['corechart', 'line'] }); google.charts.setOnLoadCallback(this.getSocialData({ days: this

Testing Async Redux Action Jest

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-08 05:48:26
问题 I'm having trouble getting the correct output from an async redux action. I am using Jest, redux-mock-adapter, and thunk as the tools. According to redux's documentation on testing async thunks (https://redux.js.org/docs/recipes/WritingTests.html#async-action-creators), my tests should be returning an array of two actions. However, my test is only returning the first action, and not the second one that should return on a successful fetch. I think I'm just missing something small here, but it

个推Node.js 微服务实践:基于容器的一站式命令行工具链

不打扰是莪最后的温柔 提交于 2019-12-07 19:35:43
作者:个推Node.js 开发工程师 之诺 背景与摘要 由于工程数量的快速增长,个推在实践基于 Node.js 的微服务开发的过程中,遇到了如下问题: 1. 每次新建项目都需要安装一次依赖,这些依赖之间基本相似却又有微妙的区别; 2. 每次新建项目都要配置一遍相似的配置(比如 tsconfig、lint 规则等); 3. 本地 Mac 环境与线上 Docker 内的 Linux 环境不一致(尤其是有 C++ 依赖的情况)。 为了解决上述问题,个推内部开发了一个命令行小工具来标准化项目初始化流程、简化配置甚至是零配置,提供基于 Docker 的一致构建、运行环境。 CLI: init, build, test & pack 新建一个 Node.js 项目的时候,我们一般会: 1. 安装许多开发依赖:TypeScript、Jest、TSLint、benchmark、typedoc 等; 2. 配置 tsconfig、lint 规则、.prettierrc 等; 3. 安装众多项目依赖:koa、lodash、sequelize、ioredis、zipkin、node-fetch 等; 4. 初始化目录结构; 5. 配置CI 脚本。 通常,我们会选择复制一个现成的项目进行修改,导致出现众多看似相似却又不完全相同的项目,比如十个项目可能会对应十种配置组合。对于同时跨多个工程的开发人员来说

Jest fails to transpile import from npm linked module

好久不见. 提交于 2019-12-07 08:45:41
问题 I have a project with multiple modules (using Lerna) and I want to use Jest to run tests. However, when I test code that uses a shared module (npm linked module via Lerna) it seems that Babel is not correctly applied and I get the following error: SyntaxError: Unexpected token import The structure of my project is like this: - my-project |- shared |- native |- web web and native require the shared module. When I go into the shared directory and run the local tests in Jest everything works

Webpack code splitting breaks jest import with vueJs components

こ雲淡風輕ζ 提交于 2019-12-07 07:35:15
问题 Jest is throwing an error when trying to load a vueJs component that has dynamic import code. Component: <script> const Modal = () => import(/* webpackChunkName: "Modal" */ "../pages/common/Modal.vue"); export default { name: "TileEditModal", components: { Modal }, data() { return }, methods: { test() { return true; } } } </script> Test: import TileEditModal from "./TileEditModal.vue" Even with no test running, just importing that component will throw the following error: return import( /*

Testing Async Redux Action Jest

不羁的心 提交于 2019-12-06 15:50:01
I'm having trouble getting the correct output from an async redux action. I am using Jest, redux-mock-adapter, and thunk as the tools. According to redux's documentation on testing async thunks ( https://redux.js.org/docs/recipes/WritingTests.html#async-action-creators ), my tests should be returning an array of two actions. However, my test is only returning the first action, and not the second one that should return on a successful fetch. I think I'm just missing something small here, but it has been bothersome to say the least. Redux Action export const getRemoveFileMetrics = cacheKey =>