Answer the question
In order to leave comments, you need to log in
Why does jest respond to import like this?
I am developing an app using create-react-app.
Today I tried to write my first test. When strategizing a test, I get a message that the tests failed to run and many letters, including
SyntaxError: Cannot use import statement outside a module
import {createLastOnlineDescription} from './OnlineStatus'
import { create } from 'domain'
function twoChar(number){
if(String(number).length==1)
return `0${number}`
else
return number
}
describe("createLastOnlineDescription for header online status test",()=>{
describe('it shuld return `только что`',()=>{
test('input now',()=>{
const input = new Date()
const result = createLastOnlineDescription(input)
expect(result).toEqual('только что')
})
test('input 1 minute ago',()=>{
const input = new Date(new Date()-60000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('только что')
})
test('input 2 minute ago',()=>{
const input = new Date(new Date()-120000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('только что')
})
test('input 3 minute ago',()=>{
const input = new Date(new Date()-180000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('только что')
})
test('input 4 minute ago',()=>{
const input = new Date(new Date()-240000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('только что')
})
})
describe('should return n `минут назад`',()=>{
test('input 6 minutes ago',()=>{
const input = new Date(new Date()-360000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('6 минут назад')
})
test('input 45 minutes ago',()=>{
const input = new Date(new Date()-2700000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('45 минут назад')
})
test('input 59 minutes ago',()=>{
const input = new Date(new Date()-3540000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('59 минут назад')
})
})
describe('should return `час назад`',()=>{
test('input 60 minutes ago',()=>{
const input = new Date(new Date()-3600000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('час назад')
})
test('input 70 minutes ago',()=>{
const input = new Date(new Date()-4200000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('час назад')
})
test('input 74 minutes ago',()=>{
const input = new Date(new Date()-4440000)
const result = createLastOnlineDescription(input)
expect(result).toEqual('час назад')
})
})
describe('should return `сегодня в...`',()=>{
test('input 1 hour 16 minutes ago',()=>{
const input = new Date(new Date()-4560000)
const result = createLastOnlineDescription(input)
const resultHour = input.getHours()
const resultMinutes = input.getMinutes()
expect(result).toEqual(`сегодня в ${twoChar(resultHour)}:${twoChar(resultMinutes)}`)
})
test('input 1 hour 59 minutes ago',()=>{
const input = new Date(new Date()-7140000)
const result = createLastOnlineDescription(input)
const resultHour = input.getHours()
const resultMinutes = input.getMinutes()
expect(result).toEqual(`сегодня в ${twoChar(resultHour)}:${twoChar(resultMinutes)}`)
})
})
})
export function createLastOnlineDescription(lastOnlineDatetime){
if(lastOnlineDatetime === 'online')
return 'online'
const timeDifference = new Date() - new Date(lastOnlineDatetime)
if(timeDifference<300000)
return `только что`
if (timeDifference<3600000)
return `${timeDifference / 60000} минут назад`
if(timeDifference<4500000)
return `час назад`
if(timeDifference<7200000)
return timeConverter(lastOnlineDatetime)
function timeConverter(UNIX_timestamp){
const a = new Date(UNIX_timestamp * 1000);
const months = ['января','февраля','марта','апреля','мая','июня','июля','августа','сентября','октября','ноября','декабря'];
const month = months[a.getMonth()];
const date = a.getDate();
const hour = twoChar(a.getHours());
const min = twoChar(a.getMinutes());
if (a.setHours(0,0,0,0) === (new Date()).setHours(0,0,0,0))
return `сегодня в ${hour}:${min}`;
function twoChar(number){
if(String(number).length==1)
return `0${number}`
else
return number
}
}
}
{
"name": "sberbank-project",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-redux": "^7.1.3",
"react-scripts": "3.2.0",
"redux": "^4.0.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "jest",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"jest": "^24.9.0"
}
}
Answer the question
In order to leave comments, you need to log in
Found a solution here: https://stackoverflow.com/questions/35756479/does-...
For those who are too lazy to follow the link, in short, I added the ".babelrc" file to the root of the project with the content
{
"env": {
"test": {
"plugins": ["@babel/plugin-transform-modules-commonjs"]
}
}
}
npm install --save-dev @babel/plugin-transform-modules-commonjs
I can't figure out where "import { create } from 'domain'" is being used. Moreover, it seems like this is a nodejs module that should not be used in tests.
Try removing this line.
it was enough for me to just create `.babelrc`, thanks for the solution!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question