Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
golden master/snapshot/approval testing library which puts the values right into your source code
You can install "inline-snapshot" via pip:
pip install inline-snapshot
snapshot(x)
mirrors x
for easy understanding.x == snapshot(...)
,
x <= snapshot(...)
,
x in snapshot(...)
, and
snapshot(...)[key]
.outsource(data)
.You can use snapshot()
instead of the value which you want to compare with.
from inline_snapshot import snapshot
def test_something():
assert 1548 * 18489 == snapshot()
You can now run the tests and record the correct values.
$ pytest --inline-snapshot=review
from inline_snapshot import snapshot
def test_something():
assert 1548 * 18489 == snapshot(28620972)
The following examples show how you can use inline-snapshot in your tests. Take a look at the documentation if you want to know more.
from inline_snapshot import snapshot, outsource, external
def test_something():
for number in range(5):
# testing for numeric limits
assert number <= snapshot(4)
assert number >= snapshot(0)
for c in "hello world":
# test if something is part of a set
assert c in snapshot(["h", "e", "l", "o", " ", "w", "r", "d"])
s = snapshot(
{
0: {"square": 0, "pow_of_two": False},
1: {"square": 1, "pow_of_two": True},
2: {"square": 4, "pow_of_two": True},
3: {"square": 9, "pow_of_two": False},
4: {"square": 16, "pow_of_two": True},
}
)
for number in range(5):
# create sub-snapshots at runtime
assert s[number]["square"] == number**2
assert s[number]["pow_of_two"] == (
(number & (number - 1) == 0) and number != 0
)
assert outsource("large string\n" * 1000) == snapshot(
external("8bf10bdf2c30*.txt")
)
assert "generates\nmultiline\nstrings" == snapshot(
"""\
generates
multiline
strings\
"""
)
snapshot()
can also be used as parameter for functions:
from inline_snapshot import snapshot
import subprocess as sp
import sys
def run_python(cmd, stdout=None, stderr=None):
result = sp.run([sys.executable, "-c", cmd], capture_output=True)
if stdout is not None:
assert result.stdout.decode() == stdout
if stderr is not None:
assert result.stderr.decode() == stderr
def test_cmd():
run_python(
"print('hello world')",
stdout=snapshot(
"""\
hello world
"""
),
stderr=snapshot(""),
)
run_python(
"1/0",
stdout=snapshot(""),
stderr=snapshot(
"""\
Traceback (most recent call last):
File "<string>", line 1, in <module>
ZeroDivisionError: division by zero
"""
),
)
inline-snapshot provides some advanced ways to work with snapshots.
I would like to know how these features are used to further improve this small library. Let me know if you've found interesting use cases for this library via twitter, fosstodon or in the github discussions.
I would like to thank my sponsors. Without them, I would not be able to invest so much time in my projects.
If you encounter any problems, please report an issue along with a detailed description.
Distributed under the terms of the MIT license, "inline-snapshot" is free and open source software.
FAQs
golden master/snapshot/approval testing library which puts the values right into your source code
We found that inline-snapshot demonstrated a healthy version release cadence and project activity because the last version was released less than 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.