Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
jest-json
Jest matchers to work with JSON strings.
Note: If you're using Jest < 27.2.5, you should stick to jest-json@^1.0
.
Add jest-json
to your Jest config:
{
"setupTestFrameworkScriptFile": "jest-json"
}
Or if you're already using another test framework, create a setup file and require each of them:
require("jest-json");
// require("some-jest-library);
Say you need to assert foo
was called with foo("url", "{'foo': 'bar', 'spam': 'eggs'}")
:
// option 1
expect(foo).toHaveBeenCalledWith(
"url",
JSON.stringify({
foo: "bar",
spam: "eggs",
})
);
This test may fail depending on how the second argument was created:
// this will pass the test:
foo(
"url",
JSON.stringify({
foo: "bar",
spam: "eggs",
})
);
// this will fail the test:
foo(
"url",
JSON.stringify({
spam: "eggs",
foo: "bar",
})
);
See this repl.it for a working example.
To fix the test you'd have to find in foo.mock.calls
the call you want, parse the JSON and call expect().toEqual({ spam: "eggs", foo: "bar" })
.
expect.jsonMatching
In the example above, you can use the expect.jsonMatching
asymmetric matcher:
expect(foo).toHaveBeenCalledWith(
"url",
expect.jsonMatching({
foo: "bar",
spam: "eggs",
})
);
You can include other asymmetric matchers inside like:
expect.jsonMatching(
expect.objectContaining({
foo: expect.stringMatching("bar")
})
)
expect().toMatchJSON()
It's just sugar for calling JSON.parse()
and then expect().toEqual()
:
expect(json).toMatchJSON(expected);
// equivalent to:
const tmp = JSON.parse(json);
expect(tmp).toEqual(expected);
FAQs
Jest matcher for working with JSON
The npm package jest-json receives a total of 15,242 weekly downloads. As such, jest-json popularity was classified as popular.
We found that jest-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.