Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Añade Jest #11

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v18.12.1
48 changes: 48 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
body { margin: 0; padding-bottom: 3rem; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }

#form { background: rgba(0, 0, 0, 0.15); padding: 0.25rem; position: fixed; bottom: 0; left: 0; right: 0; display: flex; height: 3rem; box-sizing: border-box; backdrop-filter: blur(10px); }
#input { border: none; padding: 0 1rem; flex-grow: 1; border-radius: 2rem; margin: 0.25rem; }
#input:focus { outline: none; }
#form > button { background: #333; border: none; padding: 0 1rem; margin: 0.25rem; border-radius: 3px; outline: none; color: #fff; }

#messages { list-style-type: none; margin: 0; padding: 0; }
#messages > li { padding: 0.5rem 1rem; }
#messages > li:nth-child(odd) { background: #efefef; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form id="form" action="">
<input id="input" autocomplete="off" /><button>Send</button>
</form>
</body>

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();

var messages = document.getElementById('messages');
var form = document.getElementById('form');
var input = document.getElementById('input');

form.addEventListener('submit', function(e) {
e.preventDefault();
if (input.value) {
socket.emit('chat message', input.value);
input.value = '';
}
});

socket.on('chat message', function(msg) {
var item = document.createElement('li');
item.textContent = msg;
messages.appendChild(item);
window.scrollTo(0, document.body.scrollHeight);
});
</script>
</html>
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);

app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) => {
console.log('a user connected');

socket.on('disconnect', () => {
console.log('user disconnected');
});

socket.on('chat message', (msg) => {
io.emit('chat message', msg);
});
});

server.listen(3000, () => {
console.log('listening on *:3000');
});
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
41 changes: 41 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "yaman",
"version": "1.0.0",
"description": "The thread-based social network",
"main": "index.js",
"directories": {
"doc": "docs"
},
"scripts": {
"start": "node index.js",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/AlexRuiz7/CC.git"
},
"keywords": [
"social",
"network",
"twitter",
"discord"
],
"author": "Alejandro Ruiz Becerra",
"license": "GPL-3.0-or-later",
"bugs": {
"url": "https://github.com/AlexRuiz7/CC/issues"
},
"homepage": "https://github.com/AlexRuiz7/CC#readme",
"engines": {
"npm": ">=8.0.0",
"node": ">=18.0.0"
},
"dependencies": {
"express": "^4.18.2",
"socket.io": "^4.5.4"
},
"devDependencies": {
"jest": "^29.3.1",
"ts-jest": "^29.0.3"
}
}
4 changes: 4 additions & 0 deletions test/sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function sum(a, b) {
return a + b;
}
module.exports = sum;
5 changes: 5 additions & 0 deletions test/sum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const sum = require('./sum');

test('adds 1 + 2 to equal 3', () => {
expect(sum(1, 2)).toBe(3);
});