JavaScript Package Management and Testing Fundamentals

0.0(0)
studied byStudied by 1 person
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/438

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

439 Terms

1
New cards

What is npm?

npm is a registry for JavaScript packages that allows developers to share and use packages, and provides private development management for organizations.

2
New cards

What is the purpose of the package.json file?

The package.json file keeps track of all the packages that a project relies on, as well as other metadata.

3
New cards

How can you generate a package.json file with default settings?

Use the command npm init -y to create a package.json file with default settings.

4
New cards

What command is used to install a package in a project?

Run npm install to install a package and add it to the node_modules folder and package.json.

5
New cards

How do you install a package as a developer dependency?

Use the command npm install -D to tag it as a developer dependency, adding it to the devDependencies in package.json.

6
New cards

What does the command npm install do?

It installs all the packages listed in the package.json file.

7
New cards

Why should the node_modules directory not be tracked by git?

Because the list of dependencies can be large, it's better to have developers use npm install to install dependencies from package.json.

8
New cards

How do you tell git to ignore a file or directory?

Create a .gitignore file and list the files or directories to ignore.

9
New cards

What happens if a specific version of a package is not provided in package.json?

Running npm update can update the dependencies to the latest available version.

10
New cards

How do you remove a package from a project?

Run npm uninstall to remove it from dependencies in package.json and the node_modules folder.

11
New cards

What is the package-lock.json file?

It is automatically generated when npm install is used and typically does not need to be edited.

12
New cards

What is the purpose of the 'scripts' property in package.json?

It lists aliases for bash commands, allowing registered scripts to be run with npm.

13
New cards

How do you run a registered script with npm?

Execute npm run

14
New cards

What is the difference between global and local package installation in npm?

Global packages are available in any working directory, while local packages are only available in a specific working directory.

15
New cards

Why is it recommended to install packages locally?

Local installations prevent compatibility issues between different projects that may require different package versions.

16
New cards

What flag is used to install a package globally?

Use the -g flag to install a package globally.

17
New cards

What is Test-Driven Development (TDD)?

TDD is a workflow where tests are written before the code they validate, encouraging small, focused development steps.

18
New cards

What are some advantages of Test-Driven Development?

Advantages include improved code quality, fewer bugs, refactoring confidence, better understanding of requirements, and living documentation.

19
New cards

What does TDD encourage in terms of code design?

It encourages clearer, more modular design by writing tests first.

20
New cards

How does TDD help with bug detection?

It catches problems early, before the code is fully integrated.

21
New cards

What role does the test suite play in TDD?

The test suite provides a reliable description of what the code is meant to do, serving as living documentation.

22
New cards

At what levels can testing occur?

Testing can happen at different levels depending on what is being checked.

23
New cards

What is Unit Testing?

Testing a small unit of code (typically a function) for specific inputs/outputs.

24
New cards

What is Integration Testing?

Testing how different parts of the system work together, such as functions calling other functions or the frontend connecting to the backend.

25
New cards

What is End-to-End Testing?

Testing the application flow as a complete system, typically used to test vital functionality such as user login or payments.

26
New cards

What is the Red-Green-Refactor Cycle in TDD?

A process that involves identifying a behavior, writing a test, running the test (red), making it pass (green), refactoring, and repeating the cycle.

27
New cards

How do you identify a behavior in TDD?

By describing what the code is expected to do from an external point of view, including writing it in plain English.

28
New cards

What are the characteristics of a good test in TDD?

A good test is focused, readable, repeatable, and independent.

29
New cards

What does the 'Red' phase in TDD signify?

Running the test to check that it fails, proving that the test is valid and that the code doesn't already pass it.

30
New cards

What does the 'Green' phase in TDD involve?

Writing the simplest possible code to make the test pass.

31
New cards

What is the purpose of the Refactor phase in TDD?

To clean up the code for clarity and remove duplication without changing the behavior.

32
New cards

What is Test Coverage?

It refers to how thoroughly tests check the possible behaviors of a function, including expected, unusual, and invalid inputs.

33
New cards

What is the Happy Path in testing?

The expected, standard use case with typical input that should work without issues.

34
New cards

What are Edge Cases in testing?

Valid but unusual inputs that sit at the limits of what the function is expected to handle.

35
New cards

What does Sad Path refer to in testing?

Invalid inputs or scenarios that the function isn't designed to handle, checking whether errors are thrown or handled gracefully.

36
New cards

What is a robust test?

A test that checks what matters without breaking unnecessarily when code changes.

37
New cards

What is an example of a brittle test?

A test that fails due to small or irrelevant changes, such as checking the exact structure of an object.

38
New cards

What is a robust alternative to a brittle test?

A test that focuses on important behavior, such as checking if an object has a specific property with a certain value.

39
New cards

What is Jest?

A JavaScript Testing Framework with a focus on simplicity.

40
New cards

How should testing frameworks be installed?

As dev dependencies using the -D flag.

41
New cards

What is the first step in the Red-Green-Refactor cycle?

Identify a behavior that describes what the code is expected to do.

42
New cards

What should you consider when choosing an example input for a behavior?

Think about typical values as well as unusual ones.

43
New cards

What should you do after a test passes in TDD?

Refactor the code to improve clarity and remove duplication.

44
New cards

What is the importance of re-running tests after refactoring?

To ensure that the tests still pass and the behavior of the code hasn't changed.

45
New cards

What is the typical order of behaviors to test in TDD?

Start with happy path behaviors, then build up to edge cases and error handling.

46
New cards

What command is used to install Jest as a development dependency?

npm i -D jest

47
New cards

How can tests be executed using a script in package.json?

By adding a script entry: "scripts": { "test": "jest" }

48
New cards

What file extensions does Jest look for by default?

.js, .jsx, .ts, .tsx

49
New cards

What suffixes do Jest test files commonly have?

.spec or .test

50
New cards

What are the names of files that Jest recognizes as test files?

Files called test.js or spec.js.

51
New cards

What is the purpose of the describe function in Jest?

To group linked behaviours and tests with a description string and a callback function.

52
New cards

What does the test function do in Jest?

It describes a specific behaviour being tested and contains assertions within a callback function.

53
New cards

What is the alias for the test function in Jest?

it

54
New cards

What happens to a test block when running a file with Jest?

It automatically passes unless an error is thrown inside it.

55
New cards

What is the purpose of Jest's assertion library?

To assert that the behaviour of a function is as expected.

56
New cards

What is the function of the expect statement in Jest?

To allow the use of semantic matcher functions to assert expected behaviour.

57
New cards

What does the matcher .toBe do in Jest?

Tests if two variables are the same.

58
New cards

What does the matcher .toEqual do in Jest?

Tests if two variables look the same.

59
New cards

What does the matcher .toContain do in Jest?

Tests if an array or iterable contains a given value.

60
New cards

What does the matcher .toMatchObject do in Jest?

Tests if an object contains given properties.

61
New cards

How can you negate expected behaviour in Jest?

By using the .not modifier with matchers.

62
New cards

What are Jest hooks used for?

To run actions before or after each test to maintain a sterile testing environment.

63
New cards

What is the purpose of beforeEach in Jest?

To register a function that runs before each test block.

64
New cards

What are the four types of Jest hooks?

beforeAll, beforeEach, afterEach, afterAll.

65
New cards

What are the seven primitive data types in JavaScript?

string, boolean, null, undefined, number, bigint, symbol.

66
New cards

How are primitive data types in JavaScript characterized?

They are fundamental data types from which more complex data structures are built.

67
New cards

Can variables in JavaScript be reassigned to different data types?

Yes, variables can be reassigned to hold a new value of any given data type at any point.

68
New cards

What is the type of the variable 'container' when it is assigned the value 'fruit'?

string

69
New cards

What happens to the type of 'container' when it is assigned the value 2?

It becomes a number.

70
New cards

What is the type of 'container' after it is assigned the value true?

boolean.

71
New cards

What does it mean that primitives are immutable in JavaScript?

Properties on primitive values cannot be created or overwritten.

72
New cards

What happens when you try to assign a property to a primitive value?

The write operation is ignored.

73
New cards

How are primitives compared in JavaScript?

Primitives are compared by value using the comparison operator ===.

74
New cards

What happens to variable 'a' and 'b' when 'a' is assigned the value 10 and then modified?

Variable 'a' becomes 20, while 'b' remains 10.

75
New cards

What distinguishes objects from primitives in JavaScript?

Objects are mutable, meaning their state or content can be updated.

76
New cards

What is the result of deleting a property from an object?

The property is removed and accessing it returns undefined.

77
New cards

How are objects stored in memory in JavaScript?

Objects are stored by reference, meaning they point to a specific address in memory.

78
New cards

What happens when two variables reference the same object?

Changes made to one variable will be reflected in the other.

79
New cards

What is the result of comparing two different object literals?

They are not equal because they reference different memory addresses.

80
New cards

What are arrays in JavaScript?

Arrays are a special type of object that are mutable and stored by reference.

81
New cards

What is the result of comparing two arrays that are identical in content but declared separately?

They are not equal because they reference different memory addresses.

82
New cards

What are some methods to create copies of objects in JavaScript?

Using a for loop, Array.prototype.slice(), or the spread operator.

83
New cards

What does the Jest assertion 'toBe' compare?

It compares the references of two arrays.

84
New cards

What does the Jest assertion 'toEqual' do?

It checks if the contents of two arrays are the same, including order.

85
New cards

What does 'deeply equal' mean in the context of Jest's 'toEqual'?

It means that the contents of the arrays are checked at every level of nesting.

86
New cards

Is there a native function in JavaScript to deeply check the contents of two objects?

No, you need to rely on third-party libraries for this functionality.

87
New cards

What is the purpose of the function createNewPeople?

To create a new array of objects with each age property replaced with a yearOfBirth property.

88
New cards

What does the test case 'returns a new array' check for in the createNewPeople function?

It checks that the output array is a different array from the input array.

89
New cards

Why is it important to ensure that createNewPeople does not mutate the input array?

To prevent unintended side effects that could alter the original input array.

90
New cards

What does the test case 'does not mutate the input array' verify?

It verifies that the contents of the input array remain unchanged after calling createNewPeople.

91
New cards

What is hoisting in JavaScript?

Hoisting is a feature where variable and function declarations are moved to the top of their scope by the JavaScript interpreter.

92
New cards

How does hoisting affect function declarations?

Function declarations are hoisted along with their definitions, allowing them to be called before their actual declaration in the code.

93
New cards

What happens when you try to call a function expression before its declaration?

It results in a ReferenceError because function expressions are not hoisted.

94
New cards

What is the Temporal Dead Zone in relation to let and const?

It is the period during which variables declared with let and const cannot be accessed until their declaration line is executed.

95
New cards

What error is thrown when trying to access a variable declared with const before its declaration?

A ReferenceError.

96
New cards

What does the console log output when a variable declared with var is accessed before its assignment?

It outputs 'undefined' instead of throwing a ReferenceError.

97
New cards

What is destructuring in JavaScript?

Destructuring is a syntax that allows unpacking values from arrays or object properties into individual variables.

98
New cards

How can object destructuring be used with a tutor object?

By using the syntax const { name, age } = tutor; to unpack properties into variables.

99
New cards

What is the result of using destructuring on the tutor object with the properties name and age?

name becomes 'David' and age becomes 31.

100
New cards

Can variable names be changed during object destructuring?

Yes, variable names can be changed for clarity or to avoid naming conflicts.