Skip to content

Instantly share code, notes, and snippets.

@cmonacaps
Last active April 23, 2020 19:15
Show Gist options
  • Save cmonacaps/c182e65113fc542f20c8bc63e998091b to your computer and use it in GitHub Desktop.
Save cmonacaps/c182e65113fc542f20c8bc63e998091b to your computer and use it in GitHub Desktop.
different ways to import, to arrive at "addTwo is not a function" ... also, obviously I should have been mocking `_utils4`

for the entire run, this was true: util.js

const getNextTaskPreviewForUser = (...
const getNextAssignedTaskPreview = ( ...
const addTwo = (x, y) => {
  console.log('real addTwo');
  return x + y;
};
export { getNextTaskPreviewForUser, getNextAssignedTaskPreview, addTwo };

test.js

    test('self', async () => {

      //  8<   8<   8<   8<   8<   8<   8<   8<   8<   8<   8<   8<   8<   8< 

      // This last thing, cannot get it to work.

      // ● <TaskDetailContainer /> › Wrt the `serverSideAssignNextTask` flag,  › self
      //
      //   expect(received).toThrow(expected)
      //
      //   Expected substring: "_"
      //
      //   Received function did not throw

      testUtils.flushPromises();
      expect(async () => {
        await (wrapper => wrapper.update())(renderWrapper());
      }).toThrow('_');
      testUtils.flushPromises();
    });
The one with "es6" style import is *a bit* more interesting than the other 2
1.
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [TypeError: addTwo is not a function]
at reportException (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at innerInvokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:332:9)
at invokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at HTMLUnknownElementImpl._dispatch (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at HTMLUnknownElementImpl.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at HTMLUnknownElement.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:144:23)
at Object.invokeGuardedCallbackDev (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:237:16)
at invokeGuardedCallback (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:292:31)
at beginWork$1 (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:23234:7)
at performUnitOfWork (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:22188:12) TypeError: addTwo is not a function
at TaskDetailContainer.render (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/container.js:888:5)
2.
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [TypeError: addTwo is not a function]
at reportException (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at innerInvokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:332:9)
at invokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at HTMLUnknownElementImpl._dispatch (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at HTMLUnknownElementImpl.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at HTMLUnknownElement.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:144:23)
at Object.invokeGuardedCallbackDev (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:237:16)
at invokeGuardedCallback (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:292:31)
at beginWork$1 (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:23234:7)
at performUnitOfWork (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:22188:12) TypeError: addTwo is not a function
at TaskDetailContainer.render (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/container.js:887:5)
3.
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [TypeError: (0 , _utils4.addTwo) is not a function]
at reportException (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at innerInvokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:332:9)
at invokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at HTMLUnknownElementImpl._dispatch (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at HTMLUnknownElementImpl.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at HTMLUnknownElement.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:144:23)
at Object.invokeGuardedCallbackDev (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:237:16)
at invokeGuardedCallback (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:292:31)
at beginWork$1 (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:23234:7)
at performUnitOfWork (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:22188:12) TypeError: (0 , _utils4.addTwo) is not a function
at TaskDetailContainer.render (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/container.js:888:5)
3.b.
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [TypeError: addTwo is not a function]
at reportException (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:62:24)
at innerInvokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:332:9)
at invokeEventListeners (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:267:3)
at HTMLUnknownElementImpl._dispatch (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:214:9)
at HTMLUnknownElementImpl.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
at HTMLUnknownElement.dispatchEvent (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/generated/EventTarget.js:144:23)
at Object.invokeGuardedCallbackDev (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:237:16)
at invokeGuardedCallback (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:292:31)
at beginWork$1 (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:23234:7)
at performUnitOfWork (/Users/collinmonahan/Documents/src/warhead/node_modules/react-dom/cjs/react-dom.development.js:22188:12) TypeError: addTwo is not a function
at TaskDetailContainer.render (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/container.js:889:5)
// *** Container.js. ... case numbers here correspond to output in cases.txt (attached)
// 1
const TaskUtils = require('components/Task/utils');
const getNextAssignedTaskPreview = TaskUtils.getNextAssignedTaskPreview;
const getNextTaskPreviewForUser = TaskUtils.getNextTaskPreviewForUser;
const addTwo = TaskUtils.addTwo;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 2
const { getNextAssignedTaskPreview, getNextTaskPreviewForUser, addTwo } =
require('components/Task/utils');
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 3
import { getNextAssignedTaskPreview, getNextTaskPreviewForUser, addTwo } from 'components/Task/utils';
// 8<. SNIP most of it
render() {
// 8<
addTwo(stringProp1, stringProp2);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 3.b.
import * as TaskUtils from 'components/Task/utils';
const { getNextAssignedTaskPreview, getNextTaskPreviewForUser, addTwo } = TaskUtils;
Note that in no case did it print 'real addTwo'. (see addendum)
1.
Error: expect(received).toThrow(expected)
Expected substring: "_"
Received function did not throw
at Object.<anonymous> (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/__tests__/TaskDetail.test.js:550:10)
at Object.asyncJestTest (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-jasmine2/build/jasmineAsyncInstall.js:100:37)
at resolve (/Users/collinmonahan/Documents/src/warhead/node_modules/jest-jasmine2/build/queueRunner.js:43:12)
at new Promise (<anonymous>)
2.
Error: expect(received).toThrow(expected)
Expected substring: "_"
Received function did not throw
at _callee4$ (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/__tests__/TaskDetail.test.js:550:10)
at tryCatch (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:274:22)
at Generator.prototype.(anonymous function) [as next] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
at /Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:32:7
at new Promise (<anonymous>)
3.
Error: expect(received).toThrow(expected)
Expected substring: "_"
Received function did not throw
at _callee4$ (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/__tests__/TaskDetail.test.js:550:10)
at tryCatch (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:274:22)
at Generator.prototype.(anonymous function) [as next] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
3.b.
Error: expect(received).toThrow(expected)
Expected substring: "_"
Received function did not throw
at _callee4$ (/Users/collinmonahan/Documents/src/warhead/src/components/Task/Detail/__tests__/TaskDetail.test.js:550:10)
at tryCatch (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:45:40)
at Generator.invoke [as _invoke] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:274:22)
at Generator.prototype.(anonymous function) [as next] (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/node_modules/regenerator-runtime/runtime.js:97:21)
at asyncGeneratorStep (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:3:24)
at _next (/Users/collinmonahan/Documents/src/warhead/node_modules/@babel/runtime/helpers/asyncToGenerator.js:25:9)
// Two Different ways tried
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mock Case 1 ( * 3 container cases already recorded in prev gist rev)
//
// *** TEST.JS ... toward the top of the file
let mockGetNextAssignedTaskPreview = jest.fn(() => {
throw new Error('gNATP');
});
let mockGetNextTaskPreviewForUser = jest.fn(() => {
throw new Error('gNTPFU');
});
let mockAddTwo = jest.fn(() => {
throw new Error('aT'); // <---------- looking for this to see if I got it working
});
const mockTaskUtils = jest.requireActual('components/Task/utils');
jest.mock('components/Task/utils', () => ({
__esModule: true,
...mockTaskUtils,
getNextAssignedTaskPreview: mockGetNextAssignedTaskPreview,
getNextTaskPreviewForUser: mockGetNextTaskPreviewForUser,
addTwo: mockAddTwo,
}));
// notably below that is:
import TaskDetailContainer from '../container';
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Mock Case 2 (* same 3 import style, attached in mock_2_cases.txt)
// *** TEST.JS ... toward the top of the file
let mockGetNextAssignedTaskPreview = jest.fn(() => {
throw new Error('gNATP');
});
let mockGetNextTaskPreviewForUser = jest.fn(() => {
throw new Error('gNTPFU');
});
let mockAddTwo = jest.fn(() => {
throw new Error('aT');
});
const mockTaskUtils = jest.requireActual('components/Task/utils');
jest.mock('components/Task/utils', () => ({
__esModule: true,
...mockTaskUtils,
getNextAssignedTaskPreview: () => mockGetNextAssignedTaskPreview,
getNextTaskPreviewForUser: () => mockGetNextTaskPreviewForUser,
addTwo: () => mockAddTwo,
}));
// notably below that is:
import TaskDetailContainer from '../container';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment