Skip to content

Commit

Permalink
JavaScript (#4)
Browse files Browse the repository at this point in the history
* JavaScript

* Fix working directory

* Fix directory

* Fix
  • Loading branch information
rogervinas authored Nov 16, 2023
1 parent ccd1c8a commit cdad7b6
Show file tree
Hide file tree
Showing 11 changed files with 3,779 additions and 1 deletion.
40 changes: 40 additions & 0 deletions .github/workflows/javascript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: JavaScript

on:
push:
branches: [ "main" ]
paths:
- javascript/**
pull_request:
branches: [ "main" ]
paths:
- javascript/**

permissions:
contents: read

jobs:
build:

runs-on: ubuntu-latest

defaults:
run:
working-directory: javascript

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Configure Node
uses: actions/setup-node@v3
with:
node-version: '18.x'
cache: 'npm'
cache-dependency-path: javascript/package-lock.json

- name: Install
run: npm install

- name: Test
run: npm test
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@ Using:

Using:
* [Kotest](https://kotest.io)
* [MockK](https://mockk.io/)
* [MockK](https://mockk.io/)

## JavaScript

[![CI](https://github.com/rogervinas/tests-everywhere/actions/workflows/javascript.yml/badge.svg)](https://github.com/rogervinas/tests-everywhere/actions/workflows/javascript.yml)
![Node](https://img.shields.io/badge/Node-18.x-blue?labelColor=black)

JavaScript testing with [Jest](https://jestjs.io/)

Pre-requisites:
* Install [Node Version Manager](https://github.com/nvm-sh/nvm)
* Install [Node.js](https://nodejs.org/en/) using `nvm use v18` or `nvm use stable`

Create project from scratch:
* Create project using `npm init`
* Install [Jest](https://jestjs.io/) using `npm install --save-dev jest`

Run this project:
* Install dependencies running `npm install`
* Test with `npm test`
* Run with `npm start`
1 change: 1 addition & 0 deletions javascript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
15 changes: 15 additions & 0 deletions javascript/HelloApp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const HelloMessage = require("./HelloMessage.js");
const HelloConsole = require("./HelloConsole.js");

class HelloApp {
constructor(helloMessage, helloConsole) {
this.helloMessage = helloMessage;
this.helloConsole = helloConsole;
}

printHello() {
this.helloConsole.print(this.helloMessage.text);
}
}

module.exports = HelloApp;
26 changes: 26 additions & 0 deletions javascript/HelloApp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const HelloMessage = require("./HelloMessage");
const HelloConsole = require("./HelloConsole");
const HelloApp = require("./HelloApp");

jest.mock('./HelloMessage');
jest.mock('./HelloConsole');

describe('HelloApp', () => {
it("should print hello message", () => {
const helloMessageText = "Hello Test!";
HelloMessage.mockImplementation(() => {
return {
text: helloMessageText
}
});
const helloMessage = new HelloMessage();

const helloConsole = new HelloConsole();

const helloApp = new HelloApp(helloMessage, helloConsole);
helloApp.printHello();

expect(helloConsole.print).toHaveBeenCalledTimes(1);
expect(helloConsole.print).toHaveBeenCalledWith(helloMessageText);
})
});
7 changes: 7 additions & 0 deletions javascript/HelloConsole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class HelloConsole {
print(text) {
console.log(text);
}
}

module.exports = HelloConsole
5 changes: 5 additions & 0 deletions javascript/HelloMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class HelloMessage {
text = "Hello World!";
}

module.exports = HelloMessage
8 changes: 8 additions & 0 deletions javascript/HelloMessage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const HelloMessage = require("./HelloMessage");

describe('HelloMessage', () => {
it("should return hello world", () => {
const helloMessage = new HelloMessage();
expect(helloMessage.text).toBe('Hello World!');
})
});
8 changes: 8 additions & 0 deletions javascript/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const HelloMessage = require("./HelloMessage");
const HelloConsole = require("./HelloConsole");
const HelloApp = require("./HelloApp");

const helloMessage = new HelloMessage();
const helloConsole = new HelloConsole();
const helloApp = new HelloApp(helloMessage, helloConsole);
helloApp.printHello();
Loading

0 comments on commit cdad7b6

Please sign in to comment.