Why do I get an error when I start JEST from the IDE?

I use pycharm, but probably not because of pycharm itself.

I have created a simple function to manipulate localstorage.

/**
* @param {string} name localstorage's key
* @param {Object} data
*/
function save (name, data) {
try {
localStorage.setItem(name, JSON.stringify(data))
} catch (e) {
console.error(e.message)
}
}

/**
* @param {string} name retreave key
*/
function load (name) {
let data = null
try {
data = JSON.parse(localStorage.getItem(name))
} catch (e) {
console.error(e.message)
return null
}

return data
}

function remove (name) {
try {
localStorage.removeItem(name)
} catch (e) {
console.error(e.message)
}
}
module.exports = {
save,
load,
remove
}

I then created a test.

const LocalStorage = require('@/service/LocalStorage')

describe('LocalStorage.js', () => {
const sampleValue = {'aaa': 'sample'}
const keyName = 'item'
beforeEach(() => {
localStorage.clear()
})
it('Cannot read a key that does not exist.', () => {
expect(LocalStorage.load(keyName)).toBeNull()
})
it('Save with a specified key and read with a key that exists.', () => {
LocalStorage.save(keyName, sampleValue)
LocalStorage.load(keyName)
expect(LocalStorage.load(keyName)).toEqual(sampleValue)
})
it('Can delete keys that exist.', () => {
LocalStorage.save(keyName, sampleValue)
LocalStorage.remove(keyName)
expect(LocalStorage.load(keyName)).toBeNull()
})
})

Okay! Test passed! :)

dev\vue> npm test test/unit/specs/LocalStorage.spec.js

However, when I start from Jest in the IDE, an error occurs.

Can anyone find the cause of this?

2 comments
Comment actions Permalink

What does your npm test script look like? Please also share a screenshot of your Jest run configuration

1
Comment actions Permalink

Elena san, thanks for response.

It is true that I am not familiar with the test setup screen.

From the "show screenshot" tip. Once again, looking at the jest description page, I see that "jest.conf" is not specified.
I tried specifying the Configuration file and it seems to work.
The problem is solved. Thank you very much.

https://www.jetbrains.com/help/pycharm/2022.1/run-debug-configuration-jest.html#ws_js_test_jest_specific_config_settings 

1

Please sign in to leave a comment.