Skip to content

Instantly share code, notes, and snippets.

@ivanoats
Created December 1, 2021 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivanoats/62c19279db26e2d4c1db9f504e7ff270 to your computer and use it in GitHub Desktop.
Save ivanoats/62c19279db26e2d4c1db9f504e7ff270 to your computer and use it in GitHub Desktop.
Error with Jest and CSV-Parse lib
#tests/pages/api/csv.test.ts
import { createMocks } from 'node-mocks-http';
import handler from '../../../src/pages/api/csv';
describe('/api/csv', () => {
test('returns Observations object', async () => {
const { req, res } = createMocks({
method: 'GET',
query: {
station: 'wpow1',
},
});
await handler(req, res);
expect(res._getStatusCode()).toBe(200);
expect(JSON.parse(res._getData())).toEqual(
expect.objectContaining({
message: 'weather data JSON',
})
);
});
});
#pages/api/csv.ts
import axios from 'axios';
import { parse } from 'csv-parse/sync';
import type { NextApiRequest, NextApiResponse } from 'next';
import metersPerSecondToMph from '../../util/convert';
import leadingZero from '../../util/leading-zero';
import NWSDateToJSDate from '../../util/nws-date-to-js-date';
...
try {
const records: Array<WeatherDataRow> = parse(rawWindData, {
columns: true,
});
const weatherData = records[0];
...
npm test
> nextweather@0.2.2 test
> jest --debug --verbose
"next/jest" is currently experimental. https://nextjs.org/docs/messages/experimental-jest-transformer
{
"configs": [
{
"automock": false,
"cache": true,
"cacheDirectory": "/private/var/folders/gy/7txtwsbj6qjfty4d94mbgf7r0000gn/T/jest_dx",
"clearMocks": false,
"coveragePathIgnorePatterns": [
"/node_modules/"
],
"cwd": "/Users/ivan/dev/nextweather",
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"extensionsToTreatAsEsm": [],
"extraGlobals": [],
"forceCoverageMatch": [],
"globals": {},
"haste": {
"computeSha1": false,
"enableSymlinks": false,
"forceNodeFilesystemAPI": false,
"throwOnModuleCollision": false
},
"injectGlobals": true,
"moduleDirectories": [
"node_modules",
"src"
],
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx",
"json",
"node"
],
"moduleNameMapper": [
[
"^.+\\.module\\.(css|sass|scss)$",
"/Users/ivan/dev/nextweather/node_modules/next/dist/build/jest/object-proxy.js"
],
[
"^.+\\.(css|sass|scss)$",
"/Users/ivan/dev/nextweather/node_modules/next/dist/build/jest/__mocks__/styleMock.js"
],
[
"^.+\\.(jpg|jpeg|png|gif|webp|avif|svg)$",
"/Users/ivan/dev/nextweather/node_modules/next/dist/build/jest/__mocks__/fileMock.js"
],
[
"^@/components/(.*)$",
"/Users/ivan/dev/nextweather/components/$1"
],
[
"\\.(scss|sass|css)$",
"identity-obj-proxy"
]
],
"modulePathIgnorePatterns": [],
"name": "f406d659f72c11864dcb2c635333662a",
"prettierPath": "prettier",
"resetMocks": false,
"resetModules": false,
"restoreMocks": false,
"rootDir": "/Users/ivan/dev/nextweather",
"roots": [
"/Users/ivan/dev/nextweather/src",
"/Users/ivan/dev/nextweather/tests"
],
"runner": "/Users/ivan/dev/nextweather/node_modules/jest-runner/build/index.js",
"setupFiles": [],
"setupFilesAfterEnv": [
"/Users/ivan/dev/nextweather/jest.setup.js"
],
"skipFilter": false,
"slowTestThreshold": 5,
"snapshotSerializers": [],
"testEnvironment": "/Users/ivan/dev/nextweather/node_modules/jest-environment-jsdom/build/index.js",
"testEnvironmentOptions": {},
"testLocationInResults": false,
"testMatch": [
"tests/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"testPathIgnorePatterns": [
"/node_modules/",
"/.next/",
"/Users/ivan/dev/nextweather/.next/",
"/Users/ivan/dev/nextweather/node_modules/"
],
"testRegex": [],
"testRunner": "/Users/ivan/dev/nextweather/node_modules/jest-circus/runner.js",
"testURL": "http://localhost",
"timers": "real",
"transform": [
[
"^.+\\.(js|jsx|ts|tsx)$",
"/Users/ivan/dev/nextweather/node_modules/babel-jest/build/index.js",
{
"presets": [
"next/babel",
"@babel/preset-typescript"
]
}
]
],
"transformIgnorePatterns": [
"/node_modules/",
"^.+\\.module\\.(css|sass|scss)$",
"/node_modules/",
"^.+\\.module\\.(css|sass|scss)$"
],
"watchPathIgnorePatterns": []
}
],
"globalConfig": {
"bail": 0,
"changedFilesWithAncestor": false,
"collectCoverage": false,
"collectCoverageFrom": [
"**/*.{js,jsx,ts,tsx}",
"!**/*.d.ts",
"!**/node_modules/**"
],
"coverageDirectory": "/Users/ivan/dev/nextweather/coverage",
"coverageProvider": "babel",
"coverageReporters": [
"json",
"text",
"lcov",
"clover"
],
"detectLeaks": false,
"detectOpenHandles": false,
"errorOnDeprecated": false,
"expand": false,
"findRelatedTests": false,
"forceExit": false,
"json": false,
"lastCommit": false,
"listTests": false,
"logHeapUsage": false,
"maxConcurrency": 5,
"maxWorkers": 7,
"noStackTrace": false,
"nonFlagArgs": [],
"notify": false,
"notifyMode": "failure-change",
"onlyChanged": false,
"onlyFailures": false,
"passWithNoTests": false,
"projects": [],
"rootDir": "/Users/ivan/dev/nextweather",
"runTestsByPath": false,
"skipFilter": false,
"testFailureExitCode": 1,
"testPathPattern": "",
"testSequencer": "/Users/ivan/dev/nextweather/node_modules/@jest/test-sequencer/build/index.js",
"updateSnapshot": "new",
"useStderr": false,
"verbose": true,
"watch": false,
"watchAll": false,
"watchman": true
},
"version": "27.4.3"
}
FAIL tests/pages/api/csv.test.ts
● Test suite failed to run
Cannot find module 'csv-parse/sync' from 'src/pages/api/csv.ts'
Require stack:
src/pages/api/csv.ts
tests/pages/api/csv.test.ts
1 | import axios from 'axios';
> 2 | import { parse } from 'csv-parse/sync';
| ^
3 | import type { NextApiRequest, NextApiResponse } from 'next';
4 |
5 | import metersPerSecondToMph from '../../util/convert';
at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
at Object.<anonymous> (src/pages/api/csv.ts:2:1)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.033 s
Ran all test suites.
@msiddiq17328
Copy link

any fix for this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment