2.0.0 • Published 2 years ago

@gabriele-salvo/test v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ULTRA BASIC FIRST NPM PACKAGE

This npm package, takes 2 numbers and return its sum!!!! WOW

let's go through the process

1. Setup your project

mkdir my-cool-package
cd my-cool-package
npm init

The npm init command will ask you a series of questions (like package name, version, description, entry point, test command, git repository, keywords, author, license, etc.) and create a package.json file based on your answers. You can also run npm init -y to accept default values and create package.json automatically.

if you dont have node.js and npm, follow this link.

2. Write your code

Write the code for your package. Let's say you're making a simple utility to add numbers:

// index.js (this is the main file you specified during npm init)

function addNumbers(a, b) {
  return a + b;
}

module.exports = addNumbers;

3. Write tests

Writing tests for your package is a key part of the development process. It ensures that your code works as expected and helps prevent future changes from breaking existing functionality.

3.1 Install Jest

npm install --save-dev jest

3.2 Configure Jest

Create a jest.config.js file at the root of your project with the following contents for more complex configurations:

module.exports = {
  verbose: true,
  // other configuration options
};

3.3 Set up your tests

Create a test file next to the code that you want to test. By convention, Jest looks for test files with any of the following popular naming conventions:

Files with .js suffix in __tests__ folders.
Files with .test.js suffix.
Files with .spec.js suffix.

For example, if you have a file named index.js, you could create a file named index.test.js for your tests.

Let's:

mkdir __tests__
cd __tests__
touch index.test.js

Then we need to configure change a tiny bit the package.json by adding:

"scripts": {
  "test": "jest"
}

3.4 Write your tests

Here's an example of what your index.test.js might look like:

const addNumbers = require("../index");

test("adds 1 + 2 to be equal 3", () => {
  expect(addNumbers(1, 2)).toBe(3);
});

3.5 Run your Tests

npm test

3.5.1 Test Coverage (optional)

Jest can also check how much of your code is covered by tests. You can run Jest with the --coverage flag to enable this feature.

npm test -- --coverage

This will create a coverage report in the coverage/ directory of your project, which you can view in your browser.

3.5.2 Continuous Integration (Optional)

You can integrate Jest with continuous integration (CI) systems to run tests automatically when you push code changes. This can be configured using services like GitHub Actions, Travis CI, CircleCI, etc.

For next time 😉

4. Document Your Package

Create a README.md file explaining what your package does, how to install it, examples on how to use it, and any other information users might need.

5. Publish Your Package

Before publishing, make sure you have an account on npm. Once you have an account and are logged in to npm on your machine (npm login), you can publish your package:

npm publish

This is what you would normally do but we are learning here soooo

THIS IS WHAT WE ARE GOING TO DO

Let's change a bit out package.json like this:

"name": "package-name",

to

"name": "@username/package-name",

where username is the one you use to npm login and remember to do the same in the package-lock.json

Finally we can publish using:

npm publish --access public

6. All Done!

gif

2.0.0

2 years ago

1.0.0

2 years ago